Canonical HTTP POST code?

前端 未结 3 1501
攒了一身酷
攒了一身酷 2021-02-05 13:10

I\'ve seen so many implementations of sending an http post, and admittedly I don\'t fully understand the underlying details to know what\'s required.

What is the

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-05 13:42

    I believe that the simple version of this would be

    var client = new WebClient();
    return client.UploadString(url, data);
    

    The System.Net.WebClient class has other useful methods that let you download or upload strings or a file, or bytes.

    Unfortunately there are (quite often) situations where you have to do more work. The above for example doesn't take care of situations where you need to authenticate against a proxy server (although it will use the default proxy configuration for IE).

    Also the WebClient doesn't support uploading of multiple files or setting (some specific) headers and sometimes you will have to go deeper and use the

    System.Web.HttpWebRequest and System.Net.HttpWebResponse instead.

提交回复
热议问题