2025-03-28 09:49:56 +08:00

144 lines
6.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace ThirdPartyClient.Method
{
/// <summary>
/// 通用的HTTP请求类
/// </summary>
public class HttpUtil
{
#region -> HTTP报文请求
/// <summary>
/// HTTP报文请求
/// </summary>
/// <param name="postDataStr">请求参数</param>
/// <param name="RequestUrl">请求url</param>
/// <param name="ContentType">资源的媒体类型默认application/x-www-form-urlencoded</param>
/// <param name="timeout">超时时间TCP连接建立的时间和整个请求完成的时间超出了设置的时间就抛出异常程序中断</param>
/// <param name="securityProtocol">服务器通信的默认安全协议众马对账通信需使用Tls12,WCF默认通道即可</param>
/// <returns></returns>
public static string HttpUrlPost(string postDataStr, string RequestUrl,
string ContentType = "application/x-www-form-urlencoded", int timeout = 0)
{
try
{
CookieContainer cookieContainer = new CookieContainer();
byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(postDataStr);
// 设置提交的相关参数
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
HttpWebRequest request = WebRequest.Create(RequestUrl) as HttpWebRequest;
if (timeout > 0)
{
request.Timeout = timeout * 1000;
}
request.Method = "POST";
request.KeepAlive = false;
request.ContentType = ContentType;
request.CookieContainer = cookieContainer;
request.ContentLength = postBytes.Length;
using (System.IO.Stream reqStream = request.GetRequestStream())
{
reqStream.Write(postBytes, 0, postBytes.Length);
}
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
//在这里对接收到的页面内容进行处理
//直到request.GetResponse()程序才开始向目标网页发送post请求
System.IO.Stream responseStream = response.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8"));
string val = reader.ReadToEnd();
return val;
}
}
catch (Exception ex)
{
SuperMap.RealEstate.Utility.ErrorLogHelper.Write(ex, "Post请求", "Post请求" + postDataStr + "" + RequestUrl);
return ex.ToString();
}
}
public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
}
#endregion
#region -> HTTP报文请求
/// <summary>
/// HTTP报文请求
/// </summary>
/// <param name="RequestUrl">请求url</param>
/// <param name="ContentType">资源的媒体类型默认application/x-www-form-urlencoded</param>
/// <param name="timeout">超时时间TCP连接建立的时间和整个请求完成的时间超出了设置的时间就抛出异常程序中断</param>
/// <returns></returns>
public static string HttpUrlGet(string RequestUrl,
string ContentType = "application/x-www-form-urlencoded", int timeout = 0)
{
try
{
// 设置提交的相关参数
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
//根据url创建HttpWebRequest对象
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(RequestUrl);
objRequest.Method = "get";
objRequest.KeepAlive = false;
objRequest.ContentType = ContentType;
if (timeout > 0)
{
objRequest.Timeout = timeout * 1000;
}
//读取服务器返回信息
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
//using作为语句用于定义一个范围在此范围的末尾将释放对象
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
//ReadToEnd适用于小文件的读取一次性的返回整个文件
string Result = sr.ReadToEnd();
sr.Close();
return Result;
}
}
catch (Exception ex)
{
SuperMap.RealEstate.Utility.ErrorLogHelper.Write(ex, "Get请求", "Get请求" + RequestUrl);
return ex.ToString();
}
}
#endregion
#region ->
/// <summary>
/// 请求移动业务的接口
/// </summary>
/// <param name="parameters">URL后面的参数</param>
/// <returns></returns>
public static string PostMobileService(string parameters, string provinceCode)
{
string reString = "";
try
{
string _BaseUrl = ConfigurationManager.AppSettings[provinceCode];
string RequestUrl = _BaseUrl + parameters;
reString = HttpUrlPost(parameters, RequestUrl);
}
catch (Exception ex)
{
reString = "error:" + ex.Message;
}
return reString;
}
#endregion
}
}