gjm:c# HttpWebRequest GET HTTP HTTPS request [reprint]

Source: Internet
Author: User

C # HttpWebRequest GET HTTP HTTPS Request

This demand comes from my recent practiced hand project, in the project I need to put some of their own published and collected web text into a place to store, if all the use of manual operation is large and cumbersome, so the Zhou public decided to use C # to achieve. In many places need to verify the user's identity in order to do the next step, which is unavoidable post request to log in, in the actual process found that some website login is https form, in the process encountered some small problems, and now share with you.
General helper Classes
Here is an auxiliary class I have written that uses HttpWebRequest in this class to send GET/HTTP/HTTPS requests, because sometimes you need to obtain authentication information (such as a cookie), so the HttpWebResponse object is returned, With the returned HttpWebResponse instance, you can get the session information returned during the login process, or you can get the response stream.
The code is as follows:

Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Text;  Using System.Net.Security;  Using System.Security.Cryptography.X509Certificates;  Using System.DirectoryServices.Protocols;  Using System.ServiceModel.Security;  Using System.Net;  Using System.IO;  Using System.IO.Compression;  Using System.Text.RegularExpressions; namespace Baiducang {//<summary>///for HTTP requests///</summary> public class Httpwebrespo nseutility {private static readonly string defaultuseragent = "mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;         SV1;. NET CLR 1.1.4322;. NET CLR 2.0.50727) "; <summary>///create HTTP requests for Get methods///</summary>//<param name= "url" > Requested url< /param>//<param name= "Timeout" > timeout for request </param>//<param name= "useragent" > Requested Client </param>//<param name= "cookies" > Cookie information sent with an HTTP request, if authentication is not required, you canis empty </param>///<returns></returns> public static HttpWebResponse Creategethttpresponse (s Tring Url,int? Timeout, string useragent,cookiecollection cookie) {if (string.             IsNullOrEmpty (URL)) {throw new ArgumentNullException ("url");             } HttpWebRequest request = WebRequest.Create (URL) as HttpWebRequest; Request.             Method = "GET"; Request.             useragent = defaultuseragent; if (!string. IsNullOrEmpty (useragent)) {request.             useragent = useragent; } if (timeout. HasValue) {request. Timeout = timeout.             Value; } if (cookie = null) {request.                 Cookiecontainer = new Cookiecontainer (); Request.             Cookiecontainer.add (cookies); } return request.         GetResponse () as HttpWebResponse; }//<summary>///HTTP request to create a POST method//</summary>//<param name= "url" > Requested url</param> <param name= "Parameters" > the parameter name and parameter value of the request post with the dictionary </param>//<param name= "Timeout" > Request time-out </param>//<param name= "useragent" > requested client browser information, can be empty </param>//<param name= "reques Tencoding "> The encoding used to send HTTP requests </param>//<param name=" cookies "> Cookie information sent with an HTTP request, or NULL if no authentication is required </ param>//<returns></returns> public static HttpWebResponse Createposthttpresponse (string Url,idictionary<string,string> parameters,int? Timeout, string useragent,encoding requestencoding,cookiecollection cookie) {if (string.             IsNullOrEmpty (URL)) {throw new ArgumentNullException ("url");     } if (Requestencoding==null) {throw new ArgumentNullException ("requestencoding");        } HttpWebRequest Request=null; If the HTTPS request is sent if (URL. StartsWith ("https", StringComparison.OrdinalIgnoreCase)) {Servicepointmanager.servercertificat                 Evalidationcallback = new Remotecertificatevalidationcallback (CheckValidationResult);                 Request = WebRequest.Create (URL) as HttpWebRequest; Request.             PROTOCOLVERSION=HTTPVERSION.VERSION10;             } else {request = WebRequest.Create (URL) as HttpWebRequest; } request.             Method = "POST"; Request.                          ContentType = "application/x-www-form-urlencoded"; if (!string. IsNullOrEmpty (useragent)) {request.             useragent = useragent; } else {request.             useragent = defaultuseragent; } if (timeout. HasValue) {request. Timeout = timeout. ValuE } if (cookie = null) {request.                 Cookiecontainer = new Cookiecontainer (); Request.             Cookiecontainer.add (cookies); }//If you need post data if (!) ( parameters==null| | Parameters.                 count==0) {StringBuilder buffer = new StringBuilder ();                 int i = 0; foreach (string key in Parameters. Keys) {if (i > 0) {buffer.                     AppendFormat ("&{0}={1}", Key, Parameters[key]); } else {buffer.                     AppendFormat ("{0}={1}", Key, Parameters[key]);                 } i++; } byte[] data = requestencoding.getbytes (buffer.                 ToString ()); using (Stream stream = Request. GetRequestStream ()) {stream. Write (DATA, 0, data.                 Length); }} return request.         GetResponse () as HttpWebResponse; } private static bool CheckValidationResult (object sender, X509Certificate certificate, X509chain chain, Sslpolicy  Errors Errors) {return true;//Always Accept}}}

From the code above, you can see that the post data is different from the HTTP and HTTPS sites, The Servercertificatevalidationcallback property of the ServicePointManager class needs to be set when the post data is to the HTTPS site, and the post to https:// passport.baidu.com/? Login is also required to set the ProtocolVersion property of the Httpwebresquest instance to Httpversion.version10 (this is not verified if all HTTPS sites need to be set), otherwise the GetResponse is called ( ) method throws "The underlying connection has been closed: The connection was closed unexpectedly." "Exception.

Examples of usage
This class is also very simple to use:
(1) Post data to HTTPS site, use it to login Baidu:

String loginurl = "Https://passport.baidu.com/?login";  String userName = "UserName";  string password = "password";  String tagurl = "http://cang.baidu.com/" +username+ "/tags";  Encoding Encoding = encoding.getencoding ("gb2312");  idictionary<string, string> parameters = new dictionary<string, string> ();  Parameters. ADD ("TPL", "FA");  Parameters. ADD ("Tpl_reg", "FA");  Parameters. Add ("U", tagurl);  Parameters. ADD ("Psp_tt", "0");  Parameters. ADD ("username", username);  Parameters. ADD ("password", password);  Parameters. ADD ("Mem_pass", "1");  HttpWebResponse response = Httpwebresponseutility.createposthttpresponse (loginurl, parameters, NULL, NULL, encoding, NULL);  

(2) Send GET request to HTTP site
The session information data returned by the server is included in the cookiestring, which can then be set by the cookie on the next login to authenticate the user's information, assuming we have successfully logged in and obtained a cookie, Then the code to send the GET request is as follows:

String userName = "UserName";  String tagurl = "http://cang.baidu.com/" +username+ "/tags";  Cookiecollection cookies = new Cookiecollection ();//How to get from response. headers["Set-cookie"]; gets and sets Cookiecollection code slightly  response = Httpwebresponseutility.creategethttpresponse ( Tagurl, NULL, NULL, cookies);  

(3) Send a POST request to the HTTP site
Take login 51CTO as an example:

String loginurl = "Http://home.51cto.com/index.php?s=/Index/doLogin";  String userName = "UserName";  string password = "password";  idictionary<string, string> parameters = new dictionary<string, string> ();  Parameters. ADD ("email", userName);  Parameters. ADD ("passwd", password);  HttpWebResponse response = Httpwebresponseutility.createposthttpresponse (loginurl, parameters, NULL, NULL, Encoding.UTF8, NULL);  

Here's a digression, CSDN's login processing is handled by HTTP://PASSPORT.CSDN.NET/AJAX/ACCOUNTHANDLER.ASHX, the handler.

Summarize
In this article just explained the use of sending requests to HTTP and HTTPS in C #, divided Get/post two ways, in order to reduce some cumbersome and mechanical coding, Zhou Gong encapsulated it as a class, send the data after the return of the HttpWebResponse object instance, Using this example, we can obtain the cookie returned by the server to continue sending the request as an authenticated user, or to read the contents of the server-side response, but pay attention to the response format and encoding when reading the response content. There is also a way to read HTML and WML content in this class (including data transmitted by the server using compression), but only for space and other reasons, omitted here. If you have the opportunity, you will continue to describe this in a future article.

gjm:c# HttpWebRequest GET HTTP HTTPS request [reprint]

Related Article

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.