86 lines
3.6 KiB
C#
86 lines
3.6 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Configuration;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Net.Security;
|
||
using System.Security.Cryptography.X509Certificates;
|
||
using System.Web;
|
||
|
||
namespace SuperMap.RealEstate.SendRec.Common
|
||
{
|
||
public class Common
|
||
{
|
||
#region 方法 -> HTTP报文请求
|
||
public static string HttpUrlPost(string postDataStr, string RequestUrl, string ContentType = "application/x-www-form-urlencoded", int timeout = 0)
|
||
{
|
||
try
|
||
{
|
||
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
|
||
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 方法 -> 请求移动业务的接口
|
||
/// <summary>
|
||
/// 请求移动业务的接口
|
||
/// </summary>
|
||
/// <param name="parameters">URL后面的参数</param>
|
||
/// <returns></returns>
|
||
public static string PostMobileService(string parameters, string provinceCode, bool GetBalance = false, bool ContainParameters = true)
|
||
{
|
||
string reString = "";
|
||
try
|
||
{
|
||
string _BaseUrl = GetBalance && ConfigurationManager.AppSettings[provinceCode + "_Balance"] != null ?
|
||
ConfigurationManager.AppSettings[provinceCode + "_Balance"] : ConfigurationManager.AppSettings[provinceCode];
|
||
string RequestUrl = ContainParameters ? _BaseUrl + parameters : _BaseUrl;
|
||
reString = Common.HttpUrlPost(parameters, RequestUrl);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
reString = "error:" + ex.Message;
|
||
}
|
||
return reString;
|
||
}
|
||
#endregion
|
||
}
|
||
} |