使用 System.Net.WebClient 发送 HTTP POST [英] Sending HTTP POST with System.Net.WebClient
问题描述
是否可以使用 System.Net.WebClient 发送带有一些表单数据的 HTTP POST?
Is it possible to send HTTP POST with some form data with System.Net.WebClient?
如果没有,是否有另一个库,如 WebClient 可以执行 HTTP POST?我知道我可以使用 System.Net.HttpWebRequest,但我正在寻找不那么冗长的东西.
If not, is there another library like WebClient that can do HTTP POST? I know I can use System.Net.HttpWebRequest, but I'm looking for something that is not as verbose.
希望它看起来像这样:
Using client As New TheHTTPLib
client.FormData("parm1") = "somevalue"
result = client.DownloadString(someurl, Method.POST)
End Using
推荐答案
基于@carlosfigueira 的回答,我进一步研究了 WebClient 的方法,发现 UploadValues,这正是我想要的:
Based on @carlosfigueira 's answer, I looked further into WebClient's methods and found UploadValues, which is exactly what I want:
Using client As New Net.WebClient
Dim reqparm As New Specialized.NameValueCollection
reqparm.Add("param1", "somevalue")
reqparm.Add("param2", "othervalue")
Dim responsebytes = client.UploadValues(someurl, "POST", reqparm)
Dim responsebody = (New Text.UTF8Encoding).GetString(responsebytes)
End Using
关键部分是:
client.UploadValues(someurl, "POST", reqparm)
它发送我输入的任何动词,它还帮助我创建一个正确的 url 编码的表单数据,我只需要提供参数作为 namevaluecollection.
It sends whatever verb I type in, and it also helps me create a properly url encoded form data, I just have to supply the parameters as a namevaluecollection.
这篇关于使用 System.Net.WebClient 发送 HTTP POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!