C#模擬Http與Https請求框架類實(shí)例
本文實(shí)例講述了C#模擬Http與Https請求框架類。分享給大家供大家參考。
具體實(shí)現(xiàn)方法如下:
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
namespace WebRequestTest
{
/// <summary>
/// 動(dòng)態(tài)類,每個(gè)實(shí)例使用單獨(dú)session
/// </summary>
public class HttpHelperNew
{
public CookieContainer cookie = new CookieContainer();
/// <summary>
/// post請求返回html
/// </summary>
/// <param name="Url"></param>
/// <param name="postDataStr"></param>
/// <returns></returns>
public string HttpPost(string Url, string postDataStr)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
//request.AllowAutoRedirect = false; //禁止自動(dòng)重定向
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
request.CookieContainer = cookie; //cookie信息由CookieContainer自行維護(hù)
Stream myRequestStream = request.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(postDataStr);
myStreamWriter.Close();
HttpWebResponse response = null;
try
{
this.SetCertificatePolicy();
response = (HttpWebResponse)request.GetResponse();
}
catch (System.Exception ex)
{
}
//獲取重定向地址
//string url1 = response.Headers["Location"];
if (response !=null)
{
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
else
{
return "error"; //post請求返回為空
}
}
/// <summary>
/// get請求獲取返回的html
/// </summary>
/// <param name="Url"></param>
/// <param name="postDataStr"></param>
/// <returns></returns>
public string HttpGet(string Url, string Querydata)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (Querydata == "" ? "" : "?") + Querydata);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
request.CookieContainer = cookie;
this.SetCertificatePolicy();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// response.Cookies = cookie.GetCookies(response.ResponseUri);
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
/// <summary>
/// 獲得響應(yīng)中的圖像
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public Stream GetResponseImage(string url)
{
Stream resst = null;
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.KeepAlive = true;
req.Method = "GET";
req.AllowAutoRedirect = true;
req.CookieContainer = cookie;
req.ContentType = "application/x-www-form-urlencoded";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.Timeout = 50000;
Encoding myEncoding = Encoding.GetEncoding("UTF-8");
this.SetCertificatePolicy();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
resst = res.GetResponseStream();
return resst;
}
catch
{
return null;
}
}
/// <summary>
/// 正則獲取匹配的第一個(gè)值
/// </summary>
/// <param name="html"></param>
/// <param name="pattern"></param>
/// <returns></returns>
public string getStringByRegex(string html,string pattern)
{
Regex re = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection matchs = re.Matches(html);
if (matchs.Count > 0)
{
return matchs[0].Groups[1].Value;
}
else
return "";
}
/// <summary>
/// 正則驗(yàn)證返回的response是否正確
/// </summary>
/// <param name="html"></param>
/// <param name="pattern"></param>
/// <returns></returns>
public bool verifyResponseHtml(string html ,string pattern)
{
Regex re = new Regex(pattern);
return re.IsMatch(html);
}
//注冊證書驗(yàn)證回調(diào)事件,在請求之前注冊
private void SetCertificatePolicy()
{
ServicePointManager.ServerCertificateValidationCallback
+= RemoteCertificateValidate;
}
/// <summary>
/// 遠(yuǎn)程證書驗(yàn)證,固定返回true
/// </summary>
private static bool RemoteCertificateValidate(object sender, X509Certificate cert,
X509Chain chain, SslPolicyErrors error)
{
return true;
}
}
}
希望本文所述對大家的C#程序設(shè)計(jì)有所幫助。
- c# 在WebBrowser中用SendMessage模擬鼠標(biāo)點(diǎn)擊
- C#模擬http 發(fā)送post或get請求的簡單實(shí)例
- C#采用mouse_event函數(shù)實(shí)現(xiàn)模擬鼠標(biāo)功能
- C#實(shí)現(xiàn)的三種模擬自動(dòng)登錄和提交POST信息的方法
- C# SendInput 模擬鼠標(biāo)操作的實(shí)現(xiàn)方法
- 使用C#發(fā)送Http請求實(shí)現(xiàn)模擬登陸實(shí)例
- C#基于socket模擬http請求的方法
- c#模擬銀行atm機(jī)示例分享
- C#模擬window操作鼠標(biāo)的方法
- C#如何使用Bogus創(chuàng)建模擬數(shù)據(jù)示例代碼
相關(guān)文章
關(guān)于C#結(jié)構(gòu)體 你需要知道的
這篇文章主要介紹了關(guān)于C#結(jié)構(gòu)體的相關(guān)知識,以及使用方法,文中代碼非常詳細(xì),幫助大家更好的參考和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06c# socket網(wǎng)絡(luò)編程接收發(fā)送數(shù)據(jù)示例代碼
這篇文章主要介紹了c# socket網(wǎng)絡(luò)編程,server端接收,client端發(fā)送數(shù)據(jù),大家參考使用吧2013-12-12C#實(shí)現(xiàn)將聊天數(shù)據(jù)發(fā)送加密
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)將聊天數(shù)據(jù)發(fā)送加密的功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12C#模擬http 發(fā)送post或get請求的簡單實(shí)例
下面小編就為大家?guī)硪黄狢#模擬http 發(fā)送post或get請求的簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06