模拟HttpPost Httpget请求

 

1、

public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{

//直接确认,否则打不开
return true;
}

public string post(string url, string postString)
{
//string postString = "userName=test&Title=test&Content=test";
// 初始化WebClient
WebClient webClient = new WebClient();
webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
// 将字符串转换成字节数组
byte[] postData = Encoding.ASCII.GetBytes(postString);
//ASP.NET 返回的页面一般是Unicode,如果是简体中文应使用
//Encoding.GetEncoding("GB2312").GetBytes(postString)
// 上传数据,返回页面的字节数组

ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
byte[] responseData = webClient.UploadData(url, "POST", postData);
//ASP.NET 返回的页面一般是Unicode,如果是简体中文应使用
//Encoding.GetEncoding("GB2312").GetString(responseData)
// 返回的将字节数组转换成字符串(HTML)
string srcString = Encoding.UTF8.GetString(responseData);
}

 

2、

public static string HttpPost(string Url, string postDataStr)
{
string retString = String.Empty;
try
{

//请求的url是https
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);

if ((!String.IsNullOrEmpty(webProxy)))
{
WebProxy proxyObject = new WebProxy(webProxy, Int32.Parse(webProxyPort));
proxyObject.Credentials = new NetworkCredential(username, password, domain);
request.Proxy = proxyObject;
}
//log.InfoFormat("username:{0};password:{1};domain:{2};webProxyPort:{3};Url:{4};PostData:{5}", username,
// password, domain, webProxyPort, Url, postDataStr);

byte[] bytes = Encoding.UTF8.GetBytes(postDataStr);

Stream myRequestStream = request.GetRequestStream();
myRequestStream.Write(bytes, 0, bytes.Length);
myRequestStream.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

//response.Cookies = cookie.GetCookies(response.ResponseUri);
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
//log.Info("doPostSuccess" + retString);
}
catch (Exception ex)
{
log.ErrorFormat("WechatService.HttpPost_Error!Url:{0};postDataStr:{1},ErrorMessage:{2}", Url, postDataStr, ex.Message);
}
return retString;
}

 

 

public static string HttpGet(string Url)
{
string retString = String.Empty;
try
{
if ("Local".Equals(Utility.Common.GetConfigValue("Environment")))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
}

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);

request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";

if ((!String.IsNullOrEmpty(webProxy)))
{
WebProxy proxyObject = new WebProxy(webProxy, Int32.Parse(webProxyPort));
proxyObject.Credentials = new NetworkCredential(username, password, domain);
request.Proxy = proxyObject;
}

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
//log.InfoFormat("WechatService.doGetSuccess_Url:{0};retString:{1}", Url, retString);
}
catch (Exception ex)
{
log.ErrorFormat("WechatService.HttpGet_Error!Url:{0};ErrorMessage:{1}", Url, ex.Message);
}
return retString;
}

posted @ 2017-04-17 16:39  Amity1006  阅读(101)  评论(0)    收藏  举报