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
///
/// 通过POST方式进行HTTP请求
///
/// 发送的数据
/// 请求地址
///
public static string HttpUrlPost(string str, string RequestUrl)
{
return HttpUrlPost(str, RequestUrl, "application/x-www-form-urlencoded");
}
///
/// 通过POST方式进行HTTP请求
///
/// 发送的数据
/// 请求地址
/// HTTP标头类型
///
public static string HttpUrlPost(string str, string RequestUrl, string ContentType)
{
return HttpUrlPost(str, RequestUrl, ContentType, 0);
}
///
/// 通过POST方式进行HTTP请求
///
/// 发送的数据
/// 请求地址
/// HTTP标头类型
/// 超时时间
///
public static string HttpUrlPost(string str, string RequestUrl, string ContentType, int timeout)
{
return HttpUrlPost(str, RequestUrl, ContentType, timeout, Encoding.UTF8);
}
///
/// 通过POST方式进行HTTP请求
///
/// 发送的数据
/// 请求地址
/// HTTP标头类型
/// 超时时间
/// 字符编码类型
///
public static string HttpUrlPost(string str, string RequestUrl, string ContentType, int timeout, Encoding encoding)
{
return HttpUrlPost(str, RequestUrl, ContentType, timeout, encoding, true);
}
///
/// 通过POST方式进行HTTP请求
///
/// 发送的数据
/// 请求地址
/// HTTP标头类型
/// 超时时间
/// 字符编码类型
/// 是否使用Expect:100-Continue
/// 调用Java服务接口报500错误时,请传false
///
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
}
}