C # mock HTTP/HTTPS Request Framework class

Source: Internet
Author: User

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>
Dynamic classes, each using a separate session
</summary>
public class Httphelpernew
{
Public Cookiecontainer cookie = new Cookiecontainer ();
<summary>
POST request returns 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; Disable automatic redirection
Request. Method = "POST";
Request. ContentType = "application/x-www-form-urlencoded";
Request. ContentLength = Encoding.UTF8.GetByteCount (POSTDATASTR);
Request. Cookiecontainer = cookie; Cookie information is self-maintained by Cookiecontainer
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)
{
}
Get REDIRECT Address
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 request returned as empty
}

}

<summary>
Get request gets the returned 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 = cookies. 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>
Get an image in response
</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>
A regular gets the first value of the match
</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>
Regular verifies that the returned response is correct
</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);
}



Registering a certificate validation callback event before the request is registered
private void Setcertificatepolicy ()
{
Servicepointmanager.servercertificatevalidationcallback
+ = Remotecertificatevalidate;
}
<summary>
Remote certificate validation, fixed return true
</summary>
private static bool Remotecertificatevalidate (object sender, X509Certificate cert,
X509chain chain, sslpolicyerrors error)
{
return true;
}

}
}

C # mock HTTP/HTTPS Request Framework class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.