如何将 HttpWebResponse impelmentation 转换为 HttpClient
How to convert HttpWebResponse impelmentation to HttpClient
我有一种服务方法,它使用 HttpWebRequest
来处理以下内容
while (ub < sentCount)
{
ub = step * (1 + (i++));
var k = (ub > sentCount) ? (sentCount) : ub; //to avoid array out of range exception(assign unitll array length if calc exceeds)
for (int j = lb; j < k; j++)
{
pnos = pnos + "," + pnosList[j].Phone;
}
pnos = pnos.Substring(1);
var sbPostData = new StringBuilder();
sbPostData.AppendFormat("authkey={0}", api.AuthenticationKey);
sbPostData.AppendFormat("&mobiles={0}", pnos);
sbPostData.AppendFormat("&message={0}", message);
sbPostData.AppendFormat("&sender={0}", api.SenderId);
sbPostData.AppendFormat("&route={0}", "default");
string sendSMSUri = api.EndPoint;
// Create HTTPWebrequest
var httpWReq = (HttpWebRequest)WebRequest.Create(sendSMSUri);
//Prepare and Add URL Encoded data
var encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(sbPostData.ToString());
//Specify post method
httpWReq.Method = "POST";
httpWReq.ContentType = "application/x-www-form-urlencoded";
//httpWReq.ContentLength = data.Length;
using (Stream stream = httpWReq.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
//Get the response
var response = (HttpWebResponse)httpWReq.GetResponse();
var reader = new StreamReader(response.GetResponseStream());
string responseString = reader.ReadToEnd();
//Close the response
reader.Close();
response.Close();
lb = ub;
pnos = string.Empty;
}
现在我需要在 HttpClient
中做同样的事情是否可以做到这一点。
我面临的问题是 HttpWebRequest
在 PCL 中不支持(C# class 库)我想将逻辑移至 PCL.
简短的回答是肯定的!
这是一个可能的实现:
while (ub < sentCount)
{
ub = step * (1 + (i++));
var k = (ub > sentCount) ? (sentCount) : ub; //to avoid array out of range exception(assign unitll array length if calc exceeds)
for (int j = lb; j < k; j++)
{
pnos = pnos + "," + pnosList[j].Phone;
}
pnos = pnos.Substring(1);
var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("authkey", api.AuthenticationKey));
postData.Add(new KeyValuePair<string, string>("mobiles", pnos));
postData.Add(new KeyValuePair<string, string>("message", message));
postData.Add(new KeyValuePair<string, string>("sender", api.SenderId));
postData.Add(new KeyValuePair<string, string>("route", "default"));
string sendSMSUri = api.EndPoint;
using (var httpClient = new HttpClient())
{
var request = new HttpRequestMessage(HttpMethod.Post, sendSMSUri) { Content = new FormUrlEncodedContent(postData) };
var response = await httpClient.SendAsync(request).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
// do your stuff
}
lb = ub;
pnos = string.Empty;
}
我有一种服务方法,它使用 HttpWebRequest
来处理以下内容
while (ub < sentCount)
{
ub = step * (1 + (i++));
var k = (ub > sentCount) ? (sentCount) : ub; //to avoid array out of range exception(assign unitll array length if calc exceeds)
for (int j = lb; j < k; j++)
{
pnos = pnos + "," + pnosList[j].Phone;
}
pnos = pnos.Substring(1);
var sbPostData = new StringBuilder();
sbPostData.AppendFormat("authkey={0}", api.AuthenticationKey);
sbPostData.AppendFormat("&mobiles={0}", pnos);
sbPostData.AppendFormat("&message={0}", message);
sbPostData.AppendFormat("&sender={0}", api.SenderId);
sbPostData.AppendFormat("&route={0}", "default");
string sendSMSUri = api.EndPoint;
// Create HTTPWebrequest
var httpWReq = (HttpWebRequest)WebRequest.Create(sendSMSUri);
//Prepare and Add URL Encoded data
var encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(sbPostData.ToString());
//Specify post method
httpWReq.Method = "POST";
httpWReq.ContentType = "application/x-www-form-urlencoded";
//httpWReq.ContentLength = data.Length;
using (Stream stream = httpWReq.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
//Get the response
var response = (HttpWebResponse)httpWReq.GetResponse();
var reader = new StreamReader(response.GetResponseStream());
string responseString = reader.ReadToEnd();
//Close the response
reader.Close();
response.Close();
lb = ub;
pnos = string.Empty;
}
现在我需要在 HttpClient
中做同样的事情是否可以做到这一点。
我面临的问题是 HttpWebRequest
在 PCL 中不支持(C# class 库)我想将逻辑移至 PCL.
简短的回答是肯定的!
这是一个可能的实现:
while (ub < sentCount)
{
ub = step * (1 + (i++));
var k = (ub > sentCount) ? (sentCount) : ub; //to avoid array out of range exception(assign unitll array length if calc exceeds)
for (int j = lb; j < k; j++)
{
pnos = pnos + "," + pnosList[j].Phone;
}
pnos = pnos.Substring(1);
var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("authkey", api.AuthenticationKey));
postData.Add(new KeyValuePair<string, string>("mobiles", pnos));
postData.Add(new KeyValuePair<string, string>("message", message));
postData.Add(new KeyValuePair<string, string>("sender", api.SenderId));
postData.Add(new KeyValuePair<string, string>("route", "default"));
string sendSMSUri = api.EndPoint;
using (var httpClient = new HttpClient())
{
var request = new HttpRequestMessage(HttpMethod.Post, sendSMSUri) { Content = new FormUrlEncodedContent(postData) };
var response = await httpClient.SendAsync(request).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
// do your stuff
}
lb = ub;
pnos = string.Empty;
}