126 lines
5.7 KiB
C#
126 lines
5.7 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Net.Security;
|
||
using System.Security.Cryptography.X509Certificates;
|
||
using System.Text;
|
||
using System.Web;
|
||
|
||
namespace DataTransferService.HttpClientHelp
|
||
{
|
||
public class ESHttpClient
|
||
{
|
||
#region 方法 -> HttpUrlPost
|
||
/// <summary>
|
||
/// 通过POST方式进行HTTP请求
|
||
/// </summary>
|
||
/// <param name="str">发送的数据</param>
|
||
/// <param name="RequestUrl">请求地址</param>
|
||
/// <returns></returns>
|
||
public static string HttpUrlPost(string str, string RequestUrl)
|
||
{
|
||
return HttpUrlPost(str, RequestUrl, "application/x-www-form-urlencoded");
|
||
}
|
||
/// <summary>
|
||
/// 通过POST方式进行HTTP请求
|
||
/// </summary>
|
||
/// <param name="str">发送的数据</param>
|
||
/// <param name="RequestUrl">请求地址</param>
|
||
/// <param name="ContentType">HTTP标头类型</param>
|
||
/// <returns></returns>
|
||
public static string HttpUrlPost(string str, string RequestUrl, string ContentType)
|
||
{
|
||
return HttpUrlPost(str, RequestUrl, ContentType, 0);
|
||
}
|
||
/// <summary>
|
||
/// 通过POST方式进行HTTP请求
|
||
/// </summary>
|
||
/// <param name="str">发送的数据</param>
|
||
/// <param name="RequestUrl">请求地址</param>
|
||
/// <param name="ContentType">HTTP标头类型</param>
|
||
/// <param name="timeout">超时时间</param>
|
||
/// <returns></returns>
|
||
public static string HttpUrlPost(string str, string RequestUrl, string ContentType, int timeout)
|
||
{
|
||
return HttpUrlPost(str, RequestUrl, ContentType, timeout, Encoding.UTF8);
|
||
}
|
||
/// <summary>
|
||
/// 通过POST方式进行HTTP请求
|
||
/// </summary>
|
||
/// <param name="str">发送的数据</param>
|
||
/// <param name="RequestUrl">请求地址</param>
|
||
/// <param name="ContentType">HTTP标头类型</param>
|
||
/// <param name="timeout">超时时间</param>
|
||
/// <param name="encoding">字符编码类型</param>
|
||
/// <returns></returns>
|
||
public static string HttpUrlPost(string str, string RequestUrl, string ContentType, int timeout, Encoding encoding)
|
||
{
|
||
return HttpUrlPost(str, RequestUrl, ContentType, timeout, encoding, true);
|
||
}
|
||
/// <summary>
|
||
/// 通过POST方式进行HTTP请求
|
||
/// </summary>
|
||
/// <param name="str">发送的数据</param>
|
||
/// <param name="RequestUrl">请求地址</param>
|
||
/// <param name="ContentType">HTTP标头类型</param>
|
||
/// <param name="timeout">超时时间</param>
|
||
/// <param name="encoding">字符编码类型</param>
|
||
/// <param name="expect100Continue">是否使用Expect:100-Continue
|
||
/// 调用Java服务接口报500错误时,请传false</param>
|
||
/// <returns></returns>
|
||
public static string HttpUrlPost(string str, string RequestUrl, string ContentType, int timeout, Encoding encoding, bool expect100Continue)
|
||
{
|
||
try
|
||
{
|
||
//SSL证书验证设置
|
||
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
|
||
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
|
||
| SecurityProtocolType.Tls
|
||
| (SecurityProtocolType)0x300 //Tls11
|
||
| (SecurityProtocolType)0xC00; //Tls12
|
||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(RequestUrl);
|
||
if (timeout > 0)
|
||
{
|
||
request.Timeout = timeout * 1000;
|
||
}
|
||
request.ProtocolVersion = HttpVersion.Version10;
|
||
request.ServicePoint.Expect100Continue = expect100Continue;
|
||
request.Method = "POST";//使用POST模式进行请求
|
||
request.KeepAlive = false;
|
||
request.ContentType = ContentType;//设置HTTP请求标头
|
||
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
|
||
CookieContainer cookieContainer = new CookieContainer();
|
||
byte[] postBytes = encoding.GetBytes(str);
|
||
request.CookieContainer = cookieContainer;
|
||
request.ContentLength = postBytes.Length;
|
||
//发送请求数据至指定服务
|
||
using (System.IO.Stream reqStream = request.GetRequestStream())
|
||
{
|
||
//LogHelper.WriteSendLog("==开始写数据==");
|
||
reqStream.Write(postBytes, 0, postBytes.Length);
|
||
//LogHelper.WriteSendLog("==写入完成==");
|
||
}
|
||
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
|
||
{
|
||
//在这里对接收到的页面内容进行处理
|
||
//直到request.GetResponse()程序才开始向目标网页发送post请求
|
||
System.IO.Stream responseStream = response.GetResponseStream();
|
||
System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, encoding);
|
||
string val = reader.ReadToEnd();
|
||
return val;
|
||
}
|
||
}
|
||
catch (WebException ex)
|
||
{
|
||
return ex.Message;
|
||
}
|
||
}
|
||
|
||
public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
|
||
{
|
||
return true;
|
||
}
|
||
#endregion
|
||
}
|
||
} |