You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
144 lines
5.3 KiB
C#
144 lines
5.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Web;
|
|
|
|
namespace DSWeb.Common.Helper
|
|
{
|
|
public static class WebRequestHelper
|
|
{
|
|
public static string DoGet(string url)
|
|
{
|
|
string responseString = "";//post返回的结果
|
|
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, err) => { return true; };
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
|
|
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
|
|
req.Method = "GET";
|
|
req.ContentLength = 0;
|
|
req.Timeout = 3000;
|
|
req.KeepAlive = false;
|
|
|
|
var response = req.GetResponse();
|
|
Stream streamResponse = response.GetResponseStream();
|
|
StreamReader streamRead = new StreamReader(streamResponse);
|
|
responseString = streamRead.ReadToEnd();
|
|
response.Close();
|
|
streamRead.Close();
|
|
|
|
return responseString;
|
|
}
|
|
|
|
public static string DoPost(string url, string json)
|
|
{
|
|
string responseString = "";//post返回的结果
|
|
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, err) => { return true; };
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
|
|
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
|
|
req.Method = "POST";
|
|
|
|
if (!string.IsNullOrWhiteSpace(json))
|
|
{
|
|
byte[] postBytes = Encoding.UTF8.GetBytes(json);
|
|
req.ContentType = "application/json; charset=utf-8";
|
|
req.ContentLength = Encoding.UTF8.GetByteCount(json);
|
|
Stream stream = req.GetRequestStream();
|
|
stream.Write(postBytes, 0, postBytes.Length);
|
|
req.Timeout = 10000;
|
|
stream.Close();
|
|
}
|
|
else
|
|
{
|
|
req.ContentLength = 0;
|
|
}
|
|
var response = req.GetResponse();
|
|
Stream streamResponse = response.GetResponseStream();
|
|
StreamReader streamRead = new StreamReader(streamResponse);
|
|
responseString = streamRead.ReadToEnd();
|
|
response.Close();
|
|
streamRead.Close();
|
|
|
|
return responseString;
|
|
}
|
|
|
|
public static string DoPost(string url, Dictionary<string, string> dic, int timeout = 10000)
|
|
{
|
|
string responseString = "";//post返回的结果
|
|
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, err) => { return true; };
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
|
|
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
|
|
req.Method = "POST";
|
|
req.Timeout = timeout;
|
|
req.Headers.Add("Accept-Encoding", "gzip, deflate");
|
|
|
|
if (dic.Count > 0)
|
|
{
|
|
string strContent = string.Empty;
|
|
foreach (var item in dic)
|
|
{
|
|
if (strContent.Length > 0)
|
|
{
|
|
strContent += "&";
|
|
}
|
|
strContent += $"{item.Key}={item.Value}";
|
|
}
|
|
|
|
byte[] postBytes = Encoding.UTF8.GetBytes(strContent);
|
|
req.ContentType = "application/x-www-form-urlencoded";
|
|
req.ContentLength = postBytes.Length;
|
|
Stream stream = req.GetRequestStream();
|
|
stream.Write(postBytes, 0, postBytes.Length);
|
|
stream.Close();
|
|
}
|
|
else
|
|
{
|
|
req.ContentLength = 0;
|
|
}
|
|
var response = req.GetResponse();
|
|
responseString = GetResponseBody((HttpWebResponse)response);
|
|
return responseString;
|
|
}
|
|
|
|
|
|
private static string GetResponseBody(HttpWebResponse response)
|
|
{
|
|
string responseBody = string.Empty;
|
|
if (response.ContentEncoding.ToLower().Contains("gzip"))
|
|
{
|
|
using (GZipStream stream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress))
|
|
{
|
|
using (StreamReader reader = new StreamReader(stream))
|
|
{
|
|
responseBody = reader.ReadToEnd();
|
|
}
|
|
}
|
|
}
|
|
else if (response.ContentEncoding.ToLower().Contains("deflate"))
|
|
{
|
|
using (DeflateStream stream = new DeflateStream(
|
|
response.GetResponseStream(), CompressionMode.Decompress))
|
|
{
|
|
using (StreamReader reader =
|
|
new StreamReader(stream, Encoding.UTF8))
|
|
{
|
|
responseBody = reader.ReadToEnd();
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
using (Stream stream = response.GetResponseStream())
|
|
{
|
|
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
|
|
{
|
|
responseBody = reader.ReadToEnd();
|
|
}
|
|
}
|
|
}
|
|
return responseBody;
|
|
}
|
|
}
|
|
} |