public static HttpClientResult httpGet(string url, IDictionary headMap)
{
string response = "";
try
{
HttpClient hc = new HttpClient();
hc.Timeout = new System.TimeSpan(300);
if (headMap != null && headMap.Count > 0)
{
foreach (string name in headMap.Keys)
{
hc.DefaultRequestHeaders.Add(name,headMap[name]);
}
}
var getTask = hc.GetAsync(new Uri(url));
HttpResponseMessage responseM = hc.GetAsync(new Uri(url)).Result;
Stream instream = responseM.Content.ReadAsStreamAsync().Result;
BufferedStream bfs = new BufferedStream(instream);
byte[] buffer = new byte[1024];
StringBuilder sb = new StringBuilder();
while (bfs.Read(buffer, 0, buffer.Length) > 0)
{
sb.Append(Encoding.GetEncoding("UTF-8").GetString(buffer));
}
response = sb.ToString();
bfs.Flush();
bfs.Close();
instream.Close();
int statusCode = (int)responseM.StatusCode;
return new HttpClientResult(statusCode, response);
}
catch (HttpRequestException e)
{
throw new HttpRequestException("HttpRequestException");
}
finally
{
}
}
HttpGet ----> var getTask = hc.GetAsync(new Uri(url));
HttpResponse --> HttpResponseMessage responseM = hc.GetAsync(new Uri(url)).Result;
大致有这么一个对应关系。

1394

被折叠的 条评论
为什么被折叠?



