How to make an HTTP POST web request

后端 未结 14 2765
有刺的猬
有刺的猬 2020-11-21 04:31

Canonical
How can I make an HTTP request and send some data using the POST method?

14条回答
  •  借酒劲吻你
    2020-11-21 05:04

    Simple GET request

    using System.Net;
    
    ...
    
    using (var wb = new WebClient())
    {
        var response = wb.DownloadString(url);
    }
    

    Simple POST request

    using System.Net;
    using System.Collections.Specialized;
    
    ...
    
    using (var wb = new WebClient())
    {
        var data = new NameValueCollection();
        data["username"] = "myUser";
        data["password"] = "myPassword";
    
        var response = wb.UploadValues(url, "POST", data);
        string responseInString = Encoding.UTF8.GetString(response);
    }
    

提交回复
热议问题