使用 C# 控制台应用程序创建 HTTP POST 请求并接收响应
我需要将数据发布到 URL (https://somesite.com),以根据我发布的参数下载 responseStrem 中的文件。 如何使用 C# 控制台应用程序来做到这一点?
参数: 文件名, 用户身份, 密码, 类型
I need to post data to a URL (https://somesite.com) to download file in responseStrem based on the parameters I posted.
How can I do that using a C# console application?
Parameters:
filename,
userid,
password,
type
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看一下 System.Net.WebClient 类,它可用于发出请求并处理响应,以及下载文件:
http://www.hanselman.com/blog/HTTPPOSTsAndHTTPGETsWithWebClientAndCAndFakingAPostBack.aspx
http://msdn.microsoft.com/en-us/library/system.net.webclient(VS.90) .aspx
Take a look at the
System.Net.WebClient
class, it can be used to issue requests and handle their responses, as well as to download files:http://www.hanselman.com/blog/HTTPPOSTsAndHTTPGETsWithWebClientAndCAndFakingAPostBack.aspx
http://msdn.microsoft.com/en-us/library/system.net.webclient(VS.90).aspx
为此,您可以简单地使用.net 中的“HttpWebRequest”和“HttpWebResponse”类。
下面是我编写的一个示例控制台应用程序,用于演示这是多么容易。
享受!
For this you can simply use the "HttpWebRequest" and "HttpWebResponse" classes in .net.
Below is a sample console app I wrote to demonstrate how easy this is.
Enjoy!
希望有帮助
Hope it helps
在使用 System.Net.WebClient 之前,我建议您查看一下 System.Net.Http.HttpClient,它是随 net 4.5 引入的,它使您的生活变得更加轻松。
另外,微软建议在本文中使用 HttpClient
http://msdn.microsoft.com/en-us/library/system.net.webclient(VS.90).aspx
示例可能如下所示:
Insted of using System.Net.WebClient I would recommend to have a look on System.Net.Http.HttpClient which was introduced with net 4.5 and makes your life much easier.
Also microsoft recommends to use the HttpClient on this article
http://msdn.microsoft.com/en-us/library/system.net.webclient(VS.90).aspx
An example could look like this: