如何发出HTTP POST web请求

规范的
如何使用POST方法发出HTTP请求并发送一些数据?

我可以做一个GET请求,但我不知道如何发出POST请求

有几种方法可以执行HTTPGETPOST请求:


方法A:HttpClient(首选)

可在以下位置获得:.NET Framework 4.5+.NET标准1.1+.NET核心1.0+

它是当前首选的方法,并且是异步和高性能的。在大多数情况下使用内置版本,但是对于非常旧的平台,有一个NuGet包

使用System.Net.Http;

设置

建议在应用程序的生命周期内实例化一个HttpClient,并共享它,除非您有特定的理由不这样做

私有静态只读HttpClient客户端=新HttpClient();

有关依赖项注入解决方案,请参见HttpClientFactory


  • POST

    var值=新字典<字符串,字符串>
    {
    {“thing1”,“你好”},
    {“thing2”,“world”}
    };
    var内容=新的FormUrlEncodedContent(值);
    var response=wait client.PostAsync(“http://www.example.com/recepticle.aspx“,内容);
    var responseString=await response.Content.ReadAsStringAsync();
    
  • GET

    var responseString=await client.GetStringAsync(“http://www.example.com/recepticle.aspx");
    

方法B:第三方库

RestSharp

  • POST

    var client=新的RestClient(“http://example.com");
    //client.Authenticator=新的HttpBasicAuthenticator(用户名、密码);
    var request=new RestRequest(“资源/{id}”);
    AddParameter(“thing1”、“Hello”);
    AddParameter(“thing2”、“world”);
    请求。添加标题(“标题”、“值”);
    request.AddFile(“文件”,路径);
    var响应=client.Post(请求);
    var content=response.content;//原始内容作为字符串
    var response2=客户。Post<人>(请求);
    var name=response2.Data.name;
    

Flurl.Http

这是一个更新的库,具有流畅的API、测试助手、在后台使用HttpClient,并且是可移植的。它可以通过NuGet获得

使用Flurl.Http;

  • POST

    var responseString=wait”http://www.example.com/recepticle.aspx"
    .posterlencodeasync(新的{thing1=“hello”,thing2=“world”})
    .ReceiveString();
    
  • GET

    var responseString=wait”http://www.example.com/recepticle.aspx"
    .GetStringAsync();
    

方法C:HttpWebRequest(不建议用于新工作)

可在以下位置获得:.NET Framework 1.1+/code>,.NET Standard 2.0+/code>,.NET Core 1.0+/code>。在.NETCore中,它主要是为了兼容性——它包装了HttpClient,性能较差,并且不会获得新功能

使用System.Net;
使用System.Text;//用于类编码
使用System.IO;//用于StreamReader

  • POST

    var request=(HttpWebRequest)WebRequest.Create(“http://www.example.com/recepticle.aspx");
    var postData=“thing1=“+Uri.EscapeDataString(“hello”);
    postData+=“&thing2=“+Uri.EscapeDataString(“世界”);
    var data=Encoding.ASCII.GetBytes(postData);
    request.Method=“POST”;
    request.ContentType=“application/x-www-form-urlencoded”;
    request.ContentLength=data.Length;
    使用(var stream=request.GetRequestStream())
    {
    stream.Write(数据,0,数据长度);
    }
    var response=(HttpWebResponse)request.GetResponse();
    var responseString=newstreamreader(response.GetResponseStream()).ReadToEnd();
    
  • GET

    var request=(HttpWebRequest)WebRequest.Create(“http://www.example.com/recepticle.aspx");
    var response=(HttpWebResponse)request.GetResponse();
    var responseString=newstreamreader(response.GetResponseStream()).ReadToEnd();
    

方法D:WebClient(不建议用于新工作)

这是围绕HttpWebRequest的包装。与HttpClient进行比较

提供于:.NET Framework 1.1+网络标准2.0+网络核心2.0+

使用System.Net;
使用System.Collections.Specialized;

  • POST

    使用(var client=new WebClient())
    {
    var values=新的NameValueCollection();
    值[“thing1”]=“hello”;
    值[“thing2”]=“世界”;
    var response=client.UploadValues(“http://www.example.com/recepticle.aspx“、价值观);
    var responseString=Encoding.Default.GetString(响应);
    }
    
  • GET

    使用(var client=new WebClient())
    {
    var responseString=client.DownloadString(“http://www.example.com/recepticle.aspx");
    }
    

发表评论