Sending HTTP POST with System.Net.WebClient

后端 未结 3 1796
自闭症患者
自闭症患者 2020-12-05 02:39

Is it possible to send HTTP POST with some form data with System.Net.WebClient?

If not, is there another library like WebClient that can do HTTP POST? I know I can u

3条回答
  •  無奈伤痛
    2020-12-05 02:56

    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
    

    The key part is this:

    client.UploadValues(someurl, "POST", reqparm)
    

    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.

提交回复
热议问题