|
|
using Quartz.Util;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.IO;
|
|
|
using System.IO.Compression;
|
|
|
using System.Linq;
|
|
|
using System.Net;
|
|
|
using System.Security.Cryptography.X509Certificates;
|
|
|
using System.Text;
|
|
|
using System.Web;
|
|
|
using System.Web.Script.Serialization;
|
|
|
|
|
|
namespace DSWeb.Areas.Dispatch.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;
|
|
|
|
|
|
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 DoGet_Header(string url, Dictionary<string, string> headdic)
|
|
|
{
|
|
|
string responseString = "";//post返回的结果
|
|
|
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, err) => { return true; };
|
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
|
|
|
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
|
|
|
req.Method = "GET";
|
|
|
|
|
|
|
|
|
if (headdic.Count > 0)
|
|
|
{
|
|
|
foreach (var item in headdic)
|
|
|
{
|
|
|
req.Headers.Add(item.Key, item.Value);
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
req.ContentLength = 0;
|
|
|
}
|
|
|
//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 DoGet_Header_Param(string url, Dictionary<string, string> headdic, Dictionary<string, string> dic)
|
|
|
{
|
|
|
var paramstr = "";
|
|
|
if (dic!=null && dic.Count > 0) {
|
|
|
foreach (var item in dic) {
|
|
|
if (paramstr != "") paramstr += "&";
|
|
|
paramstr += item.Key+"="+item.Value;
|
|
|
}
|
|
|
}
|
|
|
if(paramstr!="") paramstr="?"+paramstr;
|
|
|
var newurl = url + paramstr;
|
|
|
|
|
|
return DoGet_Header(newurl, headdic);
|
|
|
}
|
|
|
|
|
|
public static string DoGet_Param(string url, Dictionary<string, string> dic)
|
|
|
{
|
|
|
var paramstr = "";
|
|
|
if (dic != null && dic.Count > 0)
|
|
|
{
|
|
|
foreach (var item in dic)
|
|
|
{
|
|
|
if (paramstr != "") paramstr += "&";
|
|
|
paramstr += item.Key + "=" + item.Value;
|
|
|
}
|
|
|
}
|
|
|
if (paramstr != "") paramstr = "?" + paramstr;
|
|
|
var newurl = url + paramstr;
|
|
|
|
|
|
return DoGet(newurl);
|
|
|
}
|
|
|
|
|
|
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 = 100000;
|
|
|
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 DoPostHead(string url, Dictionary<string, string> headdic, 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 (headdic.Count > 0)
|
|
|
{
|
|
|
foreach (var item in headdic)
|
|
|
{
|
|
|
req.Headers.Add(item.Key, item.Value);
|
|
|
}
|
|
|
}
|
|
|
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 = 100000;
|
|
|
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, string json,string certificatepath,string password)
|
|
|
{
|
|
|
string responseString = "";//post返回的结果
|
|
|
var req = (HttpWebRequest)WebRequest.Create(url);
|
|
|
var certificate = new X509Certificate2(certificatepath, password);
|
|
|
req.ClientCertificates.Add(certificate);
|
|
|
req.Method = "POST";
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(json))
|
|
|
{
|
|
|
byte[] postBytes = Encoding.UTF8.GetBytes(json);
|
|
|
req.ContentType = "application/json";
|
|
|
req.ContentLength = postBytes.Length;
|
|
|
using (var stream = req.GetRequestStream())
|
|
|
{
|
|
|
stream.Write(postBytes, 0, postBytes.Length);
|
|
|
}
|
|
|
//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_DSWMS(string url, Dictionary<string, string> dic, int timeout = 10000)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
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.ContentType = "application/json";
|
|
|
req.Timeout = timeout;
|
|
|
req.Headers.Add("Accept-Encoding", "gzip, deflate");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (dic.Count > 0)
|
|
|
{
|
|
|
using (var streamWriter = new StreamWriter(req.GetRequestStream()))
|
|
|
{
|
|
|
string json = new JavaScriptSerializer().Serialize(
|
|
|
dic
|
|
|
);
|
|
|
|
|
|
streamWriter.Write(json);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
req.ContentLength = 0;
|
|
|
}
|
|
|
var response = req.GetResponse();
|
|
|
responseString = GetResponseBody((HttpWebResponse)response);
|
|
|
return responseString;
|
|
|
}
|
|
|
catch (Exception e) {
|
|
|
return e.Message;
|
|
|
}
|
|
|
}
|
|
|
public static string DoPost_Header(string url, Dictionary<string, string> dic, int timeout = 10000)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
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.ContentType = "application/json";
|
|
|
req.Timeout = timeout;
|
|
|
req.Headers.Add("Accept-Encoding", "gzip, deflate");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (dic.Count > 0)
|
|
|
{
|
|
|
foreach (var item in dic) {
|
|
|
req.Headers.Add(item.Key,item.Value);
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
req.ContentLength = 0;
|
|
|
}
|
|
|
var response = req.GetResponse();
|
|
|
responseString = GetResponseBody((HttpWebResponse)response);
|
|
|
return responseString;
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
return e.Message;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static string DoPost_Header(string url, Dictionary<string, string> dic, object obj, int timeout = 10000)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
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.ContentType = "application/json";
|
|
|
req.Timeout = timeout;
|
|
|
//req.Headers.Add("Accept-Encoding", "gzip, deflate");
|
|
|
|
|
|
if (dic.Count > 0)
|
|
|
{
|
|
|
foreach (var item in dic)
|
|
|
{
|
|
|
req.Headers.Add(item.Key, item.Value);
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
req.ContentLength = 0;
|
|
|
}
|
|
|
|
|
|
if (obj != null)
|
|
|
{
|
|
|
using (var streamWriter = new StreamWriter(req.GetRequestStream()))
|
|
|
{
|
|
|
string json = new JavaScriptSerializer().Serialize(
|
|
|
obj
|
|
|
);
|
|
|
|
|
|
streamWriter.Write(json);
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
req.ContentLength = 0;
|
|
|
}
|
|
|
|
|
|
var response = req.GetResponse();
|
|
|
responseString = GetResponseBody((HttpWebResponse)response);
|
|
|
return responseString;
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
return e.Message;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//目前仅用于荣圣达泛微OA对接,用application/x-www-form-urlencoded传递
|
|
|
public static string DoPost_Header(string url, Dictionary<string, string> dic, Dictionary<string, string> valuedic)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
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 (valuedic!=null)
|
|
|
{
|
|
|
if (dic.Count > 0)
|
|
|
{
|
|
|
foreach (var item in dic)
|
|
|
{
|
|
|
req.Headers.Add(item.Key, item.Value);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
req.ContentType = "application/x-www-form-urlencoded";
|
|
|
|
|
|
StringBuilder paraStrBuilder = new StringBuilder();
|
|
|
foreach (string key in valuedic.Keys)
|
|
|
{
|
|
|
paraStrBuilder.AppendFormat("{0}={1}&", key, valuedic[key]);
|
|
|
}
|
|
|
string para = paraStrBuilder.ToString();
|
|
|
if (para.EndsWith("&"))
|
|
|
para = para.Remove(para.Length - 1, 1);
|
|
|
|
|
|
byte[] bt = Encoding.UTF8.GetBytes(para);
|
|
|
string responseData = String.Empty;
|
|
|
req.ContentLength = bt.Length;
|
|
|
//GetRequestStream 输入流数据
|
|
|
using (Stream reqStream = req.GetRequestStream())
|
|
|
{
|
|
|
reqStream.Write(bt, 0, bt.Length);
|
|
|
reqStream.Close();
|
|
|
}
|
|
|
|
|
|
|
|
|
req.Timeout = 10000;
|
|
|
|
|
|
}
|
|
|
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;
|
|
|
}
|
|
|
catch (Exception e) {
|
|
|
return e.Message;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static string DoPost_Json(string url, object obj, int timeout = 10000)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
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.ContentType = "application/json";
|
|
|
req.Timeout = timeout;
|
|
|
req.Headers.Add("Accept-Encoding", "gzip, deflate");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (obj != null)
|
|
|
{
|
|
|
using (var streamWriter = new StreamWriter(req.GetRequestStream()))
|
|
|
{
|
|
|
string json = new JavaScriptSerializer().Serialize(
|
|
|
obj
|
|
|
);
|
|
|
|
|
|
streamWriter.Write(json);
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
req.ContentLength = 0;
|
|
|
}
|
|
|
var response = req.GetResponse();
|
|
|
responseString = GetResponseBody((HttpWebResponse)response);
|
|
|
return responseString;
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
return e.Message;
|
|
|
}
|
|
|
}
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} |