简体   繁体   English

使用 POST 方法将 HttpWebRequest 转换为 HttpClient

[英]Convert HttpWebRequest to HttpClient with POST method

I try to convert HttpWebRequest to HttpClient but without success.我尝试将 HttpWebRequest 转换为 HttpClient 但没有成功。 Can anybody help me?有谁能够帮助我?

It is my simple code with HttpWebRequest:这是我使用 HttpWebRequest 的简单代码:

string url = "https://www.somesite.com/Service";
string postData = "text to send";
var data = Encoding.ASCII.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.Proxy = null;
request.AllowAutoRedirect = false;
request.UserAgent = "Mozilla/5.0";
request.ContentType = "text/x-gwt-rpc; charset=UTF-8";
request.Headers.Add("Cookie", SetCookie);//get it after login
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream()); 
string responseText = reader.ReadToEnd();

I think you can convert you HttpWebRequest based code to HttpClient based like this:我认为您可以将基于HttpWebRequest的代码转换为基于HttpClient的代码,如下所示:

string url = "https://www.somesite.com/Service";
string postData = "text to send";
var data = Encoding.ASCII.GetBytes(postData);
var content = new ByteArrayContent(data);

using var httpHandler = new HttpClientHandler { UseCookies =  false, AllowAutoRedirect = false };
using var client = new HttpClient(httpHandler);
client.DefaultRequestHeaders.Add("UserAgent","Mozilla/5.0");
client.DefaultRequestHeaders.Add("ContentType", "text/x-gwt-rpc; charset=UTF-8");
client.DefaultRequestHeaders.Add("Cookie", SetCookie);

using var requestMessage = new HttpRequestMessage(HttpMethod.Post, url) { Content = content };

var response = await client.SendAsync(requestMessage);
var responseText = await response.Content.ReadAsStringAsync();

Remarks:评论:

  1. Instead of writing the Request's Stream manually you can use the ByteArrayContent abstraction for this.您可以为此使用ByteArrayContent抽象,而不是手动编写请求的流。 ( Related SO topic ) 相关 SO 主题
  2. In order to set the cookie(s) manually you have to turn-off the default behaviour.为了手动设置 cookie,您必须关闭默认行为。 You can do this via the HttpClientHandler's UseCookies .您可以通过HttpClientHandler 的 UseCookies来做到这一点 ( Related SO topic ) 相关 SO 主题
  3. To set the headers manually you can use the HttpClient's DefaultRequestHeaders ( Related SO topic )要手动设置标头,您可以使用HttpClient 的 DefaultRequestHeaders相关 SO 主题
  4. The counterpart of GetResponse is the SendAsync GetResponse的对应物是SendAsync
  5. Instead of reading the Response's Stream manually you can use the HttpContent's ReadAsStringAsync ( Related SO topic )您可以使用HttpContent 的 ReadAsStringAsync相关 SO 主题),而不是手动读取响应的流

UPDATE : Include OP's amended code更新:包括 OP 的修改代码

var content = new StringContent(postData, Encoding.UTF8, "text/x-gwt-rpc");

So, instead of ByteArrayContent StringContent is being used.因此,正在使用StringContent而不是ByteArrayContent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM