Sending HTTP POST with System.Net.WebClient

后端 未结 3 1797
自闭症患者
自闭症患者 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 03:13

    As far as the http verb is concerned the WebRequest might be easier. You could go for something like:

        WebRequest r = WebRequest.Create("http://some.url");
        r.Method = "POST";
        using (var s = r.GetResponse().GetResponseStream())
        {
            using (var reader = new StreamReader(r, FileMode.Open))
            {
                var content = reader.ReadToEnd();
            }
        }
    

    Obviously this lacks exception handling and writing the request body (for which you can use r.GetRequestStream() and write it like a regular stream, but I hope it may be of some help.

提交回复
热议问题