12 changed files with 966 additions and 3 deletions
@ -0,0 +1,606 @@ |
|||||||
|
using Microsoft.IdentityModel.Logging; |
||||||
|
using Newtonsoft.Json; |
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Linq; |
||||||
|
using System.Net.Cache; |
||||||
|
using System.Net; |
||||||
|
using System.Text; |
||||||
|
using System.Threading.Tasks; |
||||||
|
using System.Web; |
||||||
|
using Elight.Utility.logs; |
||||||
|
|
||||||
|
namespace Elight.Utility |
||||||
|
{ |
||||||
|
public class HttpHelper |
||||||
|
{ |
||||||
|
public static string Post(string url, object obj_model) |
||||||
|
{ |
||||||
|
ServicePointManager.DefaultConnectionLimit = 200; |
||||||
|
return Post(url, obj_model, out string error); |
||||||
|
} |
||||||
|
/// <summary> |
||||||
|
/// 提交Post数据 json |
||||||
|
/// </summary> |
||||||
|
/// <param name="url"></param> |
||||||
|
/// <param name="obj_model"></param> |
||||||
|
/// <param name="headersdic"></param> |
||||||
|
/// <returns></returns> |
||||||
|
public static string Post(string sUrl, dynamic obj_model, out string ErrMsg) |
||||||
|
{ |
||||||
|
string sPostData = JsonConvert.SerializeObject(obj_model); |
||||||
|
ErrMsg = sPostData; |
||||||
|
if (string.IsNullOrEmpty(sUrl)) |
||||||
|
return "{}"; |
||||||
|
Encoding myEncoding = Encoding.UTF8; |
||||||
|
string sMode = "POST"; |
||||||
|
sPostData = sPostData.Replace("_params", "params"); |
||||||
|
string sContentType = "application/json"; |
||||||
|
HttpWebRequest req = null; |
||||||
|
HttpWebResponse res = null; |
||||||
|
try |
||||||
|
{ |
||||||
|
req = HttpWebRequest.Create(sUrl) as HttpWebRequest; |
||||||
|
|
||||||
|
req.Method = sMode; |
||||||
|
req.Accept = "*/*"; |
||||||
|
req.KeepAlive = true; |
||||||
|
req.Timeout = 70000; |
||||||
|
req.ReadWriteTimeout = 300000; |
||||||
|
req.ServicePoint.Expect100Continue = false; |
||||||
|
req.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore); |
||||||
|
if (0 == string.Compare("POST", sMode)) |
||||||
|
{ |
||||||
|
byte[] bufPost = myEncoding.GetBytes(sPostData); |
||||||
|
req.ContentType = sContentType; |
||||||
|
req.ContentLength = bufPost.Length; |
||||||
|
using (Stream newStream = req.GetRequestStream()) |
||||||
|
{ |
||||||
|
newStream.Write(bufPost, 0, bufPost.Length); |
||||||
|
} |
||||||
|
} |
||||||
|
res = req.GetResponse() as HttpWebResponse; |
||||||
|
req.Abort(); |
||||||
|
var re = ""; |
||||||
|
using (Stream resStream = res.GetResponseStream()) |
||||||
|
{ |
||||||
|
using (StreamReader resStreamReader = new StreamReader(resStream, myEncoding)) |
||||||
|
{ |
||||||
|
re = resStreamReader.ReadToEnd(); |
||||||
|
} |
||||||
|
} |
||||||
|
res.Close(); |
||||||
|
return re; |
||||||
|
} |
||||||
|
catch (System.Net.WebException ex) |
||||||
|
{ |
||||||
|
Encoding encoding = Encoding.UTF8; |
||||||
|
var strResponse = GetResponseAsString((System.Net.HttpWebResponse)ex.Response, encoding);//这样获取web服务器返回数据 |
||||||
|
ErrMsg = ex.Message; |
||||||
|
LogService.WriteLog(ex, "接口调用"); |
||||||
|
return "{\"Message\":\"" + ErrMsg + "\",\"IsSucceed\":false}"; |
||||||
|
} |
||||||
|
finally |
||||||
|
{ |
||||||
|
if (req != null) |
||||||
|
{ |
||||||
|
req.Abort(); |
||||||
|
req = null; |
||||||
|
} |
||||||
|
if (res != null) |
||||||
|
{ |
||||||
|
res.Close(); |
||||||
|
res.Dispose(); |
||||||
|
req = null; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 提交Post数据 json 7000毫秒停止 |
||||||
|
/// </summary> |
||||||
|
/// <param name="url"></param> |
||||||
|
/// <param name="obj_model"></param> |
||||||
|
/// <param name="headersdic"></param> |
||||||
|
/// <returns></returns> |
||||||
|
public static string Post6000(string sUrl, dynamic obj_model, out string ErrMsg) |
||||||
|
{ |
||||||
|
string sPostData = JsonConvert.SerializeObject(obj_model); |
||||||
|
ErrMsg = sPostData; |
||||||
|
if (string.IsNullOrEmpty(sUrl)) |
||||||
|
return "{}"; |
||||||
|
Encoding myEncoding = Encoding.UTF8; |
||||||
|
string sMode = "POST"; |
||||||
|
sPostData = sPostData.Replace("_params", "params"); |
||||||
|
string sContentType = "application/json"; |
||||||
|
HttpWebRequest req = null; |
||||||
|
HttpWebResponse res = null; |
||||||
|
try |
||||||
|
{ |
||||||
|
req = HttpWebRequest.Create(sUrl) as HttpWebRequest; |
||||||
|
|
||||||
|
req.Method = sMode; |
||||||
|
req.Accept = "*/*"; |
||||||
|
req.KeepAlive = true; |
||||||
|
req.Timeout = 6000; |
||||||
|
req.ReadWriteTimeout = 300000; |
||||||
|
req.ServicePoint.Expect100Continue = false; |
||||||
|
req.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore); |
||||||
|
if (0 == string.Compare("POST", sMode)) |
||||||
|
{ |
||||||
|
byte[] bufPost = myEncoding.GetBytes(sPostData); |
||||||
|
req.ContentType = sContentType; |
||||||
|
req.ContentLength = bufPost.Length; |
||||||
|
using (Stream newStream = req.GetRequestStream()) |
||||||
|
{ |
||||||
|
newStream.Write(bufPost, 0, bufPost.Length); |
||||||
|
} |
||||||
|
} |
||||||
|
res = req.GetResponse() as HttpWebResponse; |
||||||
|
req.Abort(); |
||||||
|
var re = ""; |
||||||
|
using (Stream resStream = res.GetResponseStream()) |
||||||
|
{ |
||||||
|
using (StreamReader resStreamReader = new StreamReader(resStream, myEncoding)) |
||||||
|
{ |
||||||
|
re = resStreamReader.ReadToEnd(); |
||||||
|
} |
||||||
|
} |
||||||
|
res.Close(); |
||||||
|
return re; |
||||||
|
} |
||||||
|
catch (System.Net.WebException ex) |
||||||
|
{ |
||||||
|
Encoding encoding = Encoding.UTF8; |
||||||
|
var strResponse = GetResponseAsString((System.Net.HttpWebResponse)ex.Response, encoding);//这样获取web服务器返回数据 |
||||||
|
ErrMsg = ex.Message; |
||||||
|
LogService.WriteLog(ex, "接口调用"); |
||||||
|
return "{\"Message\":\"" + ErrMsg + "\",\"IsSucceed\":false}"; |
||||||
|
} |
||||||
|
finally |
||||||
|
{ |
||||||
|
if (req != null) |
||||||
|
{ |
||||||
|
req.Abort(); |
||||||
|
req = null; |
||||||
|
} |
||||||
|
if (res != null) |
||||||
|
{ |
||||||
|
res.Close(); |
||||||
|
res.Dispose(); |
||||||
|
req = null; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
/// <summary> |
||||||
|
/// 提交Post数据 byte[] |
||||||
|
/// </summary> |
||||||
|
/// <param name="url"></param> |
||||||
|
/// <param name="postBase64String">Base64字符串</param> |
||||||
|
/// <param name="headersdic"></param> |
||||||
|
/// <returns></returns> |
||||||
|
public static string PostBytes(string sUrl, string postBase64String, out string ErrMsg) |
||||||
|
{ |
||||||
|
ErrMsg = postBase64String; |
||||||
|
if (string.IsNullOrEmpty(sUrl)) |
||||||
|
return "{}"; |
||||||
|
Encoding myEncoding = Encoding.UTF8; |
||||||
|
string sMode = "POST"; |
||||||
|
string sContentType = "application/json"; |
||||||
|
HttpWebRequest req = null; |
||||||
|
HttpWebResponse res = null; |
||||||
|
try |
||||||
|
{ |
||||||
|
req = HttpWebRequest.Create(sUrl) as HttpWebRequest; |
||||||
|
|
||||||
|
req.Method = sMode; |
||||||
|
req.Accept = "*/*"; |
||||||
|
req.KeepAlive = true; |
||||||
|
req.Timeout = 70000; |
||||||
|
req.ReadWriteTimeout = 300000; |
||||||
|
req.ServicePoint.Expect100Continue = false; |
||||||
|
req.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore); |
||||||
|
if (0 == string.Compare("POST", sMode)) |
||||||
|
{ |
||||||
|
byte[] bufPost = Convert.FromBase64String(postBase64String); |
||||||
|
req.ContentType = sContentType; |
||||||
|
req.ContentLength = bufPost.Length; |
||||||
|
using (Stream newStream = req.GetRequestStream()) |
||||||
|
{ |
||||||
|
newStream.Write(bufPost, 0, bufPost.Length); |
||||||
|
} |
||||||
|
} |
||||||
|
res = req.GetResponse() as HttpWebResponse; |
||||||
|
req.Abort(); |
||||||
|
var re = ""; |
||||||
|
using (Stream resStream = res.GetResponseStream()) |
||||||
|
{ |
||||||
|
using (var mstream = new MemoryStream()) |
||||||
|
{ |
||||||
|
resStream.CopyTo(mstream); |
||||||
|
re = Convert.ToBase64String(mstream.ToArray()); |
||||||
|
} |
||||||
|
} |
||||||
|
res.Close(); |
||||||
|
return re; |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
ErrMsg = ex.Message; |
||||||
|
LogService.WriteLog(ex, "接口调用"); |
||||||
|
return "{\"Message\":\"" + ErrMsg + "\",\"IsSucceed\":false}"; |
||||||
|
} |
||||||
|
finally |
||||||
|
{ |
||||||
|
if (req != null) |
||||||
|
{ |
||||||
|
req.Abort(); |
||||||
|
req = null; |
||||||
|
} |
||||||
|
if (res != null) |
||||||
|
{ |
||||||
|
res.Close(); |
||||||
|
res.Dispose(); |
||||||
|
req = null; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 发送数据 |
||||||
|
/// </summary> |
||||||
|
/// <param name="url"></param> |
||||||
|
/// <param name="dic"></param> |
||||||
|
/// <returns></returns> |
||||||
|
public static string Post(string url, Dictionary<string, string> dic, out string ErrMsg) |
||||||
|
{ |
||||||
|
string result = ""; |
||||||
|
ErrMsg = ""; |
||||||
|
HttpWebResponse resp = null; |
||||||
|
try |
||||||
|
{ |
||||||
|
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); |
||||||
|
req.Timeout = 70000; |
||||||
|
req.ReadWriteTimeout = 300000; |
||||||
|
req.Method = "POST"; |
||||||
|
req.ContentType = "application/x-www-form-urlencoded"; |
||||||
|
#region 添加Post 参数 |
||||||
|
StringBuilder builder = new StringBuilder(); |
||||||
|
int i = 0; |
||||||
|
foreach (var item in dic) |
||||||
|
{ |
||||||
|
if (i > 0) |
||||||
|
builder.Append("&"); |
||||||
|
builder.AppendFormat("{0}={1}", item.Key, item.Value); |
||||||
|
i++; |
||||||
|
} |
||||||
|
byte[] data = Encoding.UTF8.GetBytes(builder.ToString()); |
||||||
|
req.ContentLength = data.Length; |
||||||
|
using (Stream reqStream = req.GetRequestStream()) |
||||||
|
{ |
||||||
|
reqStream.Write(data, 0, data.Length); |
||||||
|
reqStream.Close(); |
||||||
|
} |
||||||
|
#endregion |
||||||
|
resp = (HttpWebResponse)req.GetResponse(); |
||||||
|
Stream stream = resp.GetResponseStream(); |
||||||
|
//获取响应内容 |
||||||
|
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) |
||||||
|
{ |
||||||
|
result = reader.ReadToEnd(); |
||||||
|
} |
||||||
|
return result; |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
ErrMsg = ex.Message; |
||||||
|
return "{\"Message\":\"" + ErrMsg + "\",\"IsSucceed\":false}"; |
||||||
|
} |
||||||
|
finally |
||||||
|
{ |
||||||
|
if (resp != null) |
||||||
|
resp.Close(); |
||||||
|
} |
||||||
|
} |
||||||
|
/// <summary> |
||||||
|
/// 提交Post数据 file |
||||||
|
/// </summary> |
||||||
|
/// <param name="sUrl"></param> |
||||||
|
/// <param name="files"></param> |
||||||
|
/// <param name="ErrMsg"></param> |
||||||
|
/// <returns></returns> |
||||||
|
public static string Post(string sUrl, Dictionary<string, Stream> files, out string ErrMsg) |
||||||
|
{ |
||||||
|
ErrMsg = "{\"message\":\"文件:" + string.Join(",", files.Keys) + " 上传失败!\",\"code\":-1}"; |
||||||
|
if (string.IsNullOrEmpty(sUrl)) |
||||||
|
return "{}"; |
||||||
|
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); |
||||||
|
byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); |
||||||
|
byte[] endbytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); |
||||||
|
Encoding myEncoding = Encoding.UTF8; |
||||||
|
string sMode = "POST"; |
||||||
|
string sContentType = "multipart/form-data; boundary=" + boundary; |
||||||
|
HttpWebRequest req; |
||||||
|
try |
||||||
|
{ |
||||||
|
req = HttpWebRequest.Create(sUrl) as HttpWebRequest; |
||||||
|
|
||||||
|
req.Method = sMode; |
||||||
|
req.Accept = "*/*"; |
||||||
|
req.KeepAlive = false; |
||||||
|
req.Timeout = 1000 * 60 * 2; |
||||||
|
req.ReadWriteTimeout = 300000; |
||||||
|
req.Credentials = CredentialCache.DefaultCredentials; |
||||||
|
req.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore); |
||||||
|
if (0 == string.Compare("POST", sMode)) |
||||||
|
{ |
||||||
|
using (var stream = req.GetRequestStream()) |
||||||
|
{ |
||||||
|
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n"; |
||||||
|
byte[] buffer = new byte[4096]; |
||||||
|
int bytesRead = 0; |
||||||
|
foreach (var file in files) |
||||||
|
{ |
||||||
|
stream.Write(boundarybytes, 0, boundarybytes.Length); |
||||||
|
string header = string.Format(headerTemplate, file.Key, Path.GetFileName(file.Key)); |
||||||
|
byte[] headerbytes = myEncoding.GetBytes(header); |
||||||
|
stream.Write(headerbytes, 0, headerbytes.Length); |
||||||
|
while ((bytesRead = file.Value.Read(buffer, 0, buffer.Length)) != 0) |
||||||
|
{ |
||||||
|
stream.Write(buffer, 0, bytesRead); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
stream.Write(endbytes, 0, endbytes.Length); |
||||||
|
} |
||||||
|
} |
||||||
|
// Response |
||||||
|
HttpWebResponse res = req.GetResponse() as HttpWebResponse; |
||||||
|
req.Abort(); |
||||||
|
var re = ""; |
||||||
|
using (Stream resStream = res.GetResponseStream()) |
||||||
|
{ |
||||||
|
using (StreamReader resStreamReader = new StreamReader(resStream, myEncoding)) |
||||||
|
{ |
||||||
|
re = resStreamReader.ReadToEnd(); |
||||||
|
} |
||||||
|
} |
||||||
|
res.Close(); |
||||||
|
return re; |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
ErrMsg = ex.Message; |
||||||
|
return "{\"Message\":\"" + ErrMsg + "\",\"IsSucceed\":false}"; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// GET请求获取信息 |
||||||
|
/// </summary> |
||||||
|
/// <param name="url"></param> |
||||||
|
/// <returns></returns> |
||||||
|
public static string Get(string url, int timeout = 70000, List<System.Net.Cookie> cookies = null) |
||||||
|
{ |
||||||
|
ServicePointManager.DefaultConnectionLimit = 10; |
||||||
|
string ret = "{}"; |
||||||
|
try |
||||||
|
{ |
||||||
|
HttpWebRequest web = (HttpWebRequest)HttpWebRequest.Create(url); |
||||||
|
|
||||||
|
web.Method = "GET"; |
||||||
|
web.Timeout = timeout; |
||||||
|
if (cookies != null && cookies.Count > 0) |
||||||
|
{ |
||||||
|
web.CookieContainer = new CookieContainer(); |
||||||
|
|
||||||
|
string host = new Uri(url).Host; |
||||||
|
foreach (System.Net.Cookie c in cookies) |
||||||
|
{ |
||||||
|
c.Domain = host; |
||||||
|
web.CookieContainer.Add(c); |
||||||
|
} |
||||||
|
} |
||||||
|
HttpWebResponse res = (HttpWebResponse)web.GetResponse(); |
||||||
|
using (Stream s = res.GetResponseStream()) |
||||||
|
{ |
||||||
|
using (StreamReader sr = new StreamReader(s, Encoding.UTF8)) |
||||||
|
{ |
||||||
|
ret = sr.ReadToEnd(); |
||||||
|
} |
||||||
|
} |
||||||
|
res.Close(); |
||||||
|
web.Abort(); |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
ret = "{\"Message\":\"" + ex.Message + "\",\"IsSucceed\":false}"; |
||||||
|
LogService.WriteLog(ex, "接口调用"); |
||||||
|
} |
||||||
|
return ret; |
||||||
|
} |
||||||
|
public static string Get(string url, Encoding encode, int timeout = 70000, List<System.Net.Cookie> cookies = null) |
||||||
|
{ |
||||||
|
ServicePointManager.DefaultConnectionLimit = 10; |
||||||
|
string ret = "{}"; |
||||||
|
try |
||||||
|
{ |
||||||
|
HttpWebRequest web = (HttpWebRequest)HttpWebRequest.Create(url); |
||||||
|
web.Method = "GET"; |
||||||
|
web.Timeout = timeout; |
||||||
|
if (cookies != null && cookies.Count > 0) |
||||||
|
{ |
||||||
|
web.CookieContainer = new CookieContainer(); |
||||||
|
|
||||||
|
string host = new Uri(url).Host; |
||||||
|
foreach (System.Net.Cookie c in cookies) |
||||||
|
{ |
||||||
|
c.Domain = host; |
||||||
|
web.CookieContainer.Add(c); |
||||||
|
} |
||||||
|
} |
||||||
|
HttpWebResponse res = (HttpWebResponse)web.GetResponse(); |
||||||
|
using (Stream s = res.GetResponseStream()) |
||||||
|
{ |
||||||
|
using (StreamReader sr = new StreamReader(s, encode)) |
||||||
|
{ |
||||||
|
ret = sr.ReadToEnd(); |
||||||
|
} |
||||||
|
} |
||||||
|
res.Close(); |
||||||
|
web.Abort(); |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
ret = "{\"Message\":\"" + ex.Message + "\",\"IsSucceed\":false}"; |
||||||
|
LogService.WriteLog(ex, "接口调用"); |
||||||
|
} |
||||||
|
return ret; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public static string Get(string url, Dictionary<string, string> data, out string errorMsg) |
||||||
|
{ |
||||||
|
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); |
||||||
|
errorMsg = string.Empty; |
||||||
|
if (data != null && data.Count > 0) |
||||||
|
{ |
||||||
|
var urlSb = new StringBuilder(); |
||||||
|
var flag = 0; |
||||||
|
foreach (var item in data) |
||||||
|
{ |
||||||
|
urlSb.Append(flag == 0 ? "?" : "&"); |
||||||
|
urlSb.Append($"{item.Key}={HttpUtility.UrlEncode(item.Value)}"); |
||||||
|
flag++; |
||||||
|
} |
||||||
|
|
||||||
|
if (url.EndsWith("/")) |
||||||
|
{ |
||||||
|
url = url.Substring(0, url.Length - 1); |
||||||
|
} |
||||||
|
url += urlSb.ToString(); |
||||||
|
} |
||||||
|
|
||||||
|
try |
||||||
|
{ |
||||||
|
var web = (HttpWebRequest)WebRequest.Create(url); |
||||||
|
web.Timeout = 70 * 1000; |
||||||
|
web.Method = "GET"; |
||||||
|
web.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"; |
||||||
|
web.Accept = "*/*"; |
||||||
|
var res = (HttpWebResponse)web.GetResponse(); |
||||||
|
using var s = res.GetResponseStream(); |
||||||
|
using var sr = new StreamReader(s, Encoding.GetEncoding("gb2312")); |
||||||
|
return sr.ReadToEnd(); |
||||||
|
} |
||||||
|
catch (Exception e) |
||||||
|
{ |
||||||
|
Console.WriteLine(e); |
||||||
|
return string.Empty; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static string Get(string url, Dictionary<string, string> data, out string errorMsg, out string outurl) |
||||||
|
{ |
||||||
|
outurl = ""; |
||||||
|
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); |
||||||
|
errorMsg = string.Empty; |
||||||
|
if (data != null && data.Count > 0) |
||||||
|
{ |
||||||
|
var urlSb = new StringBuilder(); |
||||||
|
var flag = 0; |
||||||
|
foreach (var item in data) |
||||||
|
{ |
||||||
|
urlSb.Append(flag == 0 ? "?" : "&"); |
||||||
|
urlSb.Append($"{item.Key}={HttpUtility.UrlEncode(item.Value)}"); |
||||||
|
flag++; |
||||||
|
} |
||||||
|
|
||||||
|
if (url.EndsWith("/")) |
||||||
|
{ |
||||||
|
url = url.Substring(0, url.Length - 1); |
||||||
|
} |
||||||
|
url += urlSb.ToString(); |
||||||
|
outurl = url; |
||||||
|
} |
||||||
|
|
||||||
|
try |
||||||
|
{ |
||||||
|
var web = (HttpWebRequest)WebRequest.Create(url); |
||||||
|
|
||||||
|
web.Timeout = 70 * 1000; |
||||||
|
web.Method = "GET"; |
||||||
|
web.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"; |
||||||
|
web.Accept = "*/*"; |
||||||
|
var res = (HttpWebResponse)web.GetResponse(); |
||||||
|
using var s = res.GetResponseStream(); |
||||||
|
using var sr = new StreamReader(s, Encoding.GetEncoding("gb2312")); |
||||||
|
return sr.ReadToEnd(); |
||||||
|
} |
||||||
|
catch (Exception e) |
||||||
|
{ |
||||||
|
Console.WriteLine(e); |
||||||
|
return string.Empty; |
||||||
|
} |
||||||
|
} |
||||||
|
public static string Get(string url, Dictionary<string, string> data, Encoding encode, out string errorMsg) |
||||||
|
{ |
||||||
|
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); |
||||||
|
errorMsg = string.Empty; |
||||||
|
if (data != null && data.Count > 0) |
||||||
|
{ |
||||||
|
var urlSb = new StringBuilder(); |
||||||
|
var flag = 0; |
||||||
|
foreach (var item in data) |
||||||
|
{ |
||||||
|
urlSb.Append(flag == 0 ? "?" : "&"); |
||||||
|
urlSb.Append($"{item.Key}={HttpUtility.UrlEncode(item.Value)}"); |
||||||
|
flag++; |
||||||
|
} |
||||||
|
|
||||||
|
if (url.EndsWith("/")) |
||||||
|
{ |
||||||
|
url = url.Substring(0, url.Length - 1); |
||||||
|
} |
||||||
|
url += urlSb.ToString(); |
||||||
|
} |
||||||
|
|
||||||
|
try |
||||||
|
{ |
||||||
|
var web = (HttpWebRequest)WebRequest.Create(url); |
||||||
|
web.Timeout = 70 * 1000; |
||||||
|
web.Method = "GET"; |
||||||
|
web.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"; |
||||||
|
web.Accept = "*/*"; |
||||||
|
var res = (HttpWebResponse)web.GetResponse(); |
||||||
|
using var s = res.GetResponseStream(); |
||||||
|
using var sr = new StreamReader(s, encode); |
||||||
|
return sr.ReadToEnd(); |
||||||
|
} |
||||||
|
catch (Exception e) |
||||||
|
{ |
||||||
|
Console.WriteLine(e); |
||||||
|
return string.Empty; |
||||||
|
} |
||||||
|
} |
||||||
|
private static string GetResponseAsString(HttpWebResponse res, Encoding encoding) |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
if (res != null) |
||||||
|
{ |
||||||
|
StreamReader sr = new StreamReader(res.GetResponseStream(), encoding); |
||||||
|
return sr.ReadToEnd(); |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
return "{\"Message\":\"空响应\",\"IsSucceed\":false}"; ; |
||||||
|
} |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
return ""; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue