如何将HttpWebResponse实现转换为HttpClient

时间:2018-11-06 08:07:47

标签: c# xamarin httpwebrequest httpclient portable-class-library

我有一种服务方法,正在将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#类库)中不支持,我想将逻辑移到PCL上。

1 个答案:

答案 0 :(得分:2)

答案很简单!

这是一个可能的实现:

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;
}