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.
DS7/DSWeb/Areas/Dispatch/Helper/WebRequestHelper.cs

355 lines
13 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Quartz.Util;
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;
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_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 = 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;
}
}
}