从HttpWebRequest切换到HttpClient [英] Switching from HttpWebRequest to HttpClient
本文介绍了从HttpWebRequest切换到HttpClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将一些更改方法从httpwebrequest转移到httpclient.我已经完成了大部分工作,但仍坚持了这一工作.有人可以帮助实现这一目标.
I am trying to move change some methods from httpwebrequest to httpclient. I have done most of the work but stuck with this one. Can someone help to achieve this.
string url = someurl;
HttpWebRequest request = CreateRequest(url);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.ServicePoint.Expect100Continue = false;
string body = @"somestring here.";
byte[] postBytes = Encoding.UTF8.GetBytes(body);
request.ContentLength = postBytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(postBytes, 0, postBytes.Length);
stream.Close();
response = (HttpWebResponse)request.GetResponse();
我需要使用HttpClient转换此方法.
I need to convert this method using HttpClient.
这是我尝试过的.
string url = someurl;
var client = new HttpClient();;
client.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));//ACCEPT header
//request.ContentType = "application/x-www-form-urlencoded";
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,url);
string body = @"somestring here...";
var content = new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded");
request.Content = content;
var ss = client.PostAsync(url,content).Result;
string str2 = await ss.Content.ReadAsStringAsync();
并且我没有让这部分工作.
and I am not getting this part to work.
string body = @"somestring here.";
byte[] postBytes = Encoding.UTF8.GetBytes(body);
request.ContentLength = postBytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(postBytes, 0, postBytes.Length);
stream.Close();
推荐答案
这是我大部分时间使用的示例客户端类.您可以使用PostAsync或SendAsync
This is the sample client class which I use most of the time. You can use either PostAsync or SendAsync
public class RestClient
{
public bool IsDisposed { get; private set; }
public HttpClient BaseClient { get; private set; }
private readonly string jsonMediaType = "application/json";
public RestClient(string hostName, string token, HttpClient client)
{
BaseClient = client;
BaseClient.BaseAddress = new Uri(hostName);
BaseClient.DefaultRequestHeaders.Add("Authorization", token);
}
public async Task<string> PostAsync(string resource, string postData)
{
StringContent strContent = new StringContent(postData, Encoding.UTF8, jsonMediaType);
HttpResponseMessage responseMessage = await BaseClient.PostAsync(resource, strContent).ConfigureAwait(false);
responseMessage.RaiseExceptionIfFailed();
return await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
}
public async Task<string> SendAsync(HttpMethod method, string resource, string token, string postData)
{
var resourceUri = new Uri(resource, UriKind.Relative);
var uri = new Uri(BaseClient.BaseAddress, resourceUri);
HttpRequestMessage request = new HttpRequestMessage(method, uri);
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
if (!string.IsNullOrEmpty(postData))
{
request.Content = new StringContent(postData, Encoding.UTF8, jsonMediaType);
}
HttpResponseMessage response = BaseClient.SendAsync(request).Result;
response.RaiseExceptionIfFailed();
return await response.Content.ReadAsStringAsync();
}
protected virtual void Dispose(bool isDisposing)
{
if (IsDisposed)
{
return;
}
if (isDisposing)
{
BaseClient.Dispose();
}
IsDisposed = true;
}
public virtual void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~RestClient()
{
Dispose(false);
}
}
这篇关于从HttpWebRequest切换到HttpClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文