You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
281 lines
9.5 KiB
281 lines
9.5 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
|
|
|
|
namespace Sog
|
|
{
|
|
public class HttpUtils
|
|
{
|
|
private static readonly long MaxResponseContentBufferSize = 256000;
|
|
private static readonly string DefaultUserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36";
|
|
|
|
|
|
//注意是非异步调用,不要搞错了,上层自己处理多线程等等解决性能问题
|
|
public static string HttpGet(string url, out bool exception, Dictionary<string, string> httpHeaders)
|
|
{
|
|
using (HttpClient httpClient = new HttpClient())
|
|
{
|
|
return HttpGet(httpClient, url, out exception, httpHeaders);
|
|
}
|
|
}
|
|
|
|
//注意是非异步调用,不要搞错了,上层自己处理多线程等等解决性能问题
|
|
public static string HttpGet(HttpClient httpClient, string url, out bool exception,
|
|
Dictionary<string, string> httpHeaders)
|
|
{
|
|
exception = false;
|
|
|
|
//.net core下面这个Limit其实是没有用的,实现和.netframwork不同
|
|
ServicePointManager.DefaultConnectionLimit = 500;
|
|
try
|
|
{
|
|
TraceLog.Debug("HttpUtils.HttpGet url:{0}", url);
|
|
|
|
httpClient.MaxResponseContentBufferSize = MaxResponseContentBufferSize;
|
|
httpClient.DefaultRequestHeaders.Add("user-agent", DefaultUserAgent);
|
|
if (httpHeaders != null)
|
|
{
|
|
foreach (var item in httpHeaders)
|
|
{
|
|
httpClient.DefaultRequestHeaders.Add(item.Key, item.Value);
|
|
}
|
|
}
|
|
|
|
httpClient.Timeout = TimeSpan.FromSeconds(6);
|
|
|
|
HttpResponseMessage response = httpClient.GetAsync(new Uri(url)).Result;
|
|
string result = response.Content.ReadAsStringAsync().Result;
|
|
|
|
TraceLog.Debug("HttpUtils.HttpGet res code {0}, msg {1}",
|
|
response.StatusCode, string.IsNullOrEmpty(result) ? "null" : result);
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
TraceLog.Exception(ex);
|
|
|
|
exception = true;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
//注意是非异步调用,不要搞错了,上层自己处理多线程等等解决性能问题
|
|
public static string HttpPost(string url, List<KeyValuePair<String, String>> paramList, out bool exception)
|
|
{
|
|
exception = false;
|
|
|
|
HttpClient httpClient = null;
|
|
//.net core下面这个Limit其实是没有用的,实现和.netframwork不同
|
|
ServicePointManager.DefaultConnectionLimit = 500;
|
|
try
|
|
{
|
|
TraceLog.Debug("HttpUtils.HttpPost url:{0}", url);
|
|
|
|
httpClient = new HttpClient();
|
|
httpClient.MaxResponseContentBufferSize = MaxResponseContentBufferSize;
|
|
httpClient.DefaultRequestHeaders.Add("user-agent", DefaultUserAgent);
|
|
|
|
httpClient.Timeout = TimeSpan.FromSeconds(10);
|
|
|
|
HttpContent content = null;
|
|
if (paramList != null)
|
|
{
|
|
// FormUrlEncodedContent 生成内容格式 Content-Type: application/x-www-form-urlencoded
|
|
content = new FormUrlEncodedContent(paramList);
|
|
}
|
|
|
|
HttpResponseMessage response = httpClient.PostAsync(new Uri(url), content).Result;
|
|
string result = response.Content.ReadAsStringAsync().Result;
|
|
|
|
TraceLog.Debug("HttpUtils.HttpPost res code {0}, msg {1}",
|
|
response.StatusCode, string.IsNullOrEmpty(result) ? "null" : result);
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
return result;
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
TraceLog.Exception(ex);
|
|
|
|
exception = true;
|
|
}
|
|
finally
|
|
{
|
|
if (httpClient != null)
|
|
{
|
|
httpClient.Dispose();
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
|
|
public static string HttpPost(string url, HttpContent content, out bool exception)
|
|
{
|
|
exception = false;
|
|
|
|
HttpClient httpClient = null;
|
|
//.net core下面这个Limit其实是没有用的,实现和.netframwork不同
|
|
ServicePointManager.DefaultConnectionLimit = 500;
|
|
try
|
|
{
|
|
TraceLog.Debug("HttpUtils.HttpPost url:{0}", url);
|
|
|
|
httpClient = new HttpClient();
|
|
httpClient.MaxResponseContentBufferSize = MaxResponseContentBufferSize;
|
|
httpClient.DefaultRequestHeaders.Add("user-agent", DefaultUserAgent);
|
|
httpClient.Timeout = TimeSpan.FromSeconds(10);
|
|
|
|
HttpResponseMessage response = httpClient.PostAsync(new Uri(url), content).Result;
|
|
string result = response.Content.ReadAsStringAsync().Result;
|
|
|
|
TraceLog.Debug("HttpUtils.HttpPost res code {0}, msg {1}",
|
|
response.StatusCode, string.IsNullOrEmpty(result) ? "null" : result);
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
TraceLog.Exception(ex);
|
|
|
|
exception = true;
|
|
}
|
|
finally
|
|
{
|
|
if (httpClient != null)
|
|
{
|
|
httpClient.Dispose();
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
|
|
public static string HttpPost(string url, HttpContent content, List<KeyValuePair<String, String>> headerList, out bool exception)
|
|
{
|
|
exception = false;
|
|
|
|
HttpClient httpClient = null;
|
|
//.net core下面这个Limit其实是没有用的,实现和.netframwork不同
|
|
ServicePointManager.DefaultConnectionLimit = 500;
|
|
try
|
|
{
|
|
TraceLog.Debug("HttpUtils.HttpPost url:{0}", url);
|
|
|
|
httpClient = new HttpClient();
|
|
httpClient.MaxResponseContentBufferSize = MaxResponseContentBufferSize;
|
|
httpClient.Timeout = TimeSpan.FromSeconds(2);
|
|
|
|
if (headerList != null)
|
|
{
|
|
foreach (var item in headerList)
|
|
{
|
|
httpClient.DefaultRequestHeaders.Add(item.Key, item.Value);
|
|
}
|
|
}
|
|
|
|
HttpResponseMessage response = httpClient.PostAsync(new Uri(url), content).Result;
|
|
string result = response.Content.ReadAsStringAsync().Result;
|
|
|
|
TraceLog.Debug("HttpUtils.HttpPost res code {0}, msg {1}",
|
|
response.StatusCode, string.IsNullOrEmpty(result) ? "null" : result);
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
TraceLog.Exception(ex);
|
|
|
|
exception = true;
|
|
}
|
|
finally
|
|
{
|
|
if (httpClient != null)
|
|
{
|
|
httpClient.Dispose();
|
|
}
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
public static string HttpPost(string url, HttpContent content,
|
|
Dictionary<string, string> httpHeaders, int timeout, out int statusCode)
|
|
{
|
|
statusCode = -1;
|
|
|
|
HttpClient httpClient = null;
|
|
//.net core下面这个Limit其实是没有用的,实现和.netframwork不同
|
|
ServicePointManager.DefaultConnectionLimit = 500;
|
|
try
|
|
{
|
|
TraceLog.Debug("HttpUtils.HttpPost url:{0}", url);
|
|
|
|
httpClient = new HttpClient();
|
|
httpClient.MaxResponseContentBufferSize = MaxResponseContentBufferSize;
|
|
httpClient.Timeout = TimeSpan.FromSeconds(timeout);
|
|
|
|
if (httpHeaders != null)
|
|
{
|
|
foreach (var item in httpHeaders)
|
|
{
|
|
httpClient.DefaultRequestHeaders.Add(item.Key, item.Value);
|
|
}
|
|
}
|
|
|
|
HttpResponseMessage response = httpClient.PostAsync(new Uri(url), content).Result;
|
|
string result = response.Content.ReadAsStringAsync().Result;
|
|
|
|
TraceLog.Debug("HttpUtils.HttpPost res code {0}, msg {1}",
|
|
response.StatusCode, string.IsNullOrEmpty(result) ? "null" : result);
|
|
|
|
statusCode = (int)response.StatusCode;
|
|
|
|
return response.IsSuccessStatusCode ? result : null;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
TraceLog.Exception(ex);
|
|
}
|
|
finally
|
|
{
|
|
if (httpClient != null)
|
|
{
|
|
httpClient.Dispose();
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
}
|
|
}
|
|
|