HttpWebRequest and httpwebrequest

Source: Internet
Author: User
Tags getstream

HttpWebRequest and httpwebrequest

The HttpWebRequest class mainly uses HTTP protocol and server interaction, and usually obtains and submits data through GET and POST methods. The following two methods are described as follows:

GET Method

GET method to complete data submission by attaching parameters in the network address, such as in the address http://www.studyofnet.com /? In hl = zh-CN, the previous part of the http://www.studyofnet.com represents the URL for data submission, and the latter part of hl = zh-CN represents the additional parameters, where hl represents a key ), zh-CN indicates the value corresponding to this key ).

The program code is as follows:

C # code Replication
  HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "http://www.studyofnet.com?hl=zh-CN" );
req.Method = "GET";
using (WebResponse wr = req.GetResponse())
{// Process the received page content here}

Use GET to submit Chinese data.

The GET method adds parameters to the network address to complete data submission. For Chinese encoding, gb2312 and utf8 are commonly used.

The code of the program that uses gb2312 encoding for access is as follows:

C # code Replication
Encoding myEncoding = Encoding.GetEncoding("gb2312");
String address = "http://www.studyofnet.com /? "+ HttpUtility. UrlEncode (" parameter 1 ", myEncoding) +" = "+ HttpUtility. UrlEncode (" value 1 ", myEncoding );
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(address);
req.Method = "GET";
using (WebResponse wr = req.GetResponse())
{// Process the received page content here}

In the above program code, we access the URL http://www.studyofnet.com in GET mode, passed the parameter "parameter 1 = value 1", because the other party cannot tell the submitted data encoding type, therefore, the encoding method should be based on the website of the other party.

 

POST method

The POST method submits data by filling in parameters in the page content. The format of parameters is the same as that of the GET method, which is similar to the structure such as hl = zh-CN & newwindow = 1.

The program code is as follows:

C # code Replication
string param = "hl=zh-CN&newwindow=1";
byte[] bs = Encoding.ASCII.GetBytes(param);
HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "http://www.studyofnet.com/" );
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bs.Length;
using (Stream reqStream = req.GetRequestStream())
{ReqStream. Write (bs, 0, bs. Length);} using (WebResponse wr = req. GetResponse () {// process the received page content here}

In the code above, we visited the http://www.studyofnet.com URL, submitted data in GET and POST methods, and received the returned page content. However, if the submitted parameters contain Chinese characters, such processing is not enough. You need to encode the parameters so that the other website can recognize them.

Use POST to submit Chinese Data

The POST method completes data submission by entering parameters in the content of the page. Because the submitted parameters can indicate the encoding method used, more compatibility can be achieved theoretically.

The code of the program that uses gb2312 encoding for access is as follows:

C # code Replication
 Encoding myEncoding = Encoding.GetEncoding("gb2312");
String param = HttpUtility. urlEncode ("parameter 1", myEncoding) + "=" + HttpUtility. urlEncode ("value 1", myEncoding) + "&" + HttpUtility. urlEncode ("parameter 2", myEncoding) + "=" + HttpUtility. urlEncode ("value 2", myEncoding );
byte[] postBytes = Encoding.ASCII.GetBytes(param);
HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "http://www.studyofnet.com/" );
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
req.ContentLength = postBytes.Length;
using (Stream reqStream = req.GetRequestStream())
{ReqStream. Write (bs, 0, bs. Length);} using (WebResponse wr = req. GetResponse () {// process the received page content here}

From the code above, we can see that when posting Chinese data, we first use the UrlEncode method to convert Chinese characters into encoded ASCII codes and then submit them to the server, the encoding method can be described during submission so that the recipient's server can correctly parse the code.

How to Use the HttpWebRequest class written in C #

C # code Replication
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
namespace HttpWeb
{// <Summary> /// Http operation class /// </summary> public static class httptest {// <summary> /// obtain the url html /// </summary> /// <param name = "URL"> URL </param> /// <returns> </returns> public static string GetHtml (string URL) {WebRequest wrt; wrt = WebRequest. create (URL); wrt. credentials = CredentialCache. defaultCredentials; WebResponse wrp; wrp = wrt. getResponse (); string reader = new StreamReader (wrp. getResponseStream (), Encoding. getEncoding ("gb2312 ")). readToEnd (); try {wrt. getResponse (). close ();} catch (WebException ex) {throw ex;} return reader ;} /// <summary> /// obtain the website cookie /// </summary> /// <param name = "URL"> URL </param> /// <param name = "cookie"> cookie </param> // <returns> </returns> public static string GetHtml (string URL, out string cookie) {WebRequest wrt; wrt = WebRequest. create (URL); wrt. credentials = CredentialCache. defaultCredentials; WebResponse wrp; wrp = wrt. getResponse (); string html = new StreamReader (wrp. getResponseStream (), Encoding. getEncoding ("gb2312 ")). readToEnd (); try {wrt. getResponse (). close ();} catch (WebException ex) {throw ex;} cookie = wrp. headers. get ("Set-Cookie"); return html;} public static string GetHtml (string URL, string postData, string cookie, out string header, string server) {return GetHtml (server, URL, postData, cookie, out header);} public static string GetHtml (string server, string URL, string postData, string cookie, out string header) {byte [] byteRequest = Encoding. getEncoding ("gb2312 "). getBytes (postData); return GetHtml (server, URL, byteRequest, cookie, out header);} public static string GetHtml (string server, string URL, byte [] byteRequest, string cookie, out string header) {byte [] bytes = gethtmlbytes (server, URL, byteRequest, cookie, out header); Stream getStream = new MemoryStream (bytes ); streamReader streamReader = new StreamReader (getStream, Encoding. getEncoding ("gb2312"); string getString = streamReader. readToEnd (); streamReader. close (); getStream. close (); return getString ;} /// <summary> /// browse in Post mode /// </summary> /// <param name = "server"> server address </param> /// <param name = "URL"> URL </param> // <param name = "byteRequest"> stream </param> // <param name = "cookie"> cookie </param> /// <param name = "header"> handle </param> /// <returns> </returns> public static byte [] gethtmlbytes (string server, string URL, byte [] byteRequest, string cookie, out string header) {long contentLength; HttpWebRequest httpWebRequest; HttpWebResponse webResponse; Stream getStream; httpWebRequest = (HttpWebRequest) HttpWebRequest. create (URL); CookieContainer co = new CookieContainer (); co. setCookies (new Uri (server), cookie); httpWebRequest. cookieContainer = co; httpWebRequest. contentType = "application/x-www-form-urlencoded"; httpWebRequest. accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd. ms-excel, application/vnd. ms-powerpoint, application/msword, */* "; httpWebRequest. referer = server; httpWebRequest. userAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon ;. net clr 1.1.4322) "; httpWebRequest. method = "Post"; httpWebRequest. contentLength = byteRequest. length; Stream stream; stream = httpWebRequest. getRequestStream (); stream. write (byteRequest, 0, byteRequest. length); stream. close (); webResponse = (HttpWebResponse) httpWebRequest. getResponse (); header = webResponse. headers. toString (); getStream = webResponse. getResponseStream (); contentLength = webResponse. contentLength; byte [] outBytes = new byte [contentLength]; outBytes = ReadFully (getStream); getStream. close (); return outBytes;} public static byte [] ReadFully (Stream stream) {byte [] buffer = new byte [128]; using (MemoryStream MS = new MemoryStream ()) {while (true) {int read = stream. read (buffer, 0, buffer. length); if (read <= 0) return ms. toArray (); ms. write (buffer, 0, read );}}} /// <summary> /// Get mode /// </summary> /// <param name = "URL"> URL </param> /// <param name = "cookie"> cookies </param> /// <param name = "header"> handle </param> /// <param name = "server"> server </ param> /// <param name = "val"> server </param> /// <returns> </returns> public static string GetHtml (string URL, string cookie, out string header, string server) {return GetHtml (URL, cookie, out header, server ,"");} /// <summary> /// Get mode browsing /// </summary> /// <param name = "URL"> Get URL </param> /// <param name = "cookie"> cookie </param> /// <param name = "header"> handle </param> /// <param name = "server"> server address </param> /// <param name = "val"> </param> /// <returns> </returns> public static string GetHtml (string URL, string cookie, out string header, string server, string val) {HttpWebRequest httpWebRequest; HttpWebResponse webResponse; Stream getStream; StreamReader streamReader; string getString = ""; httpWebRequest = (HttpWebRequest) HttpWebRequest. create (URL); httpWebRequest. accept = "*/*"; httpWebRequest. referer = server; CookieContainer co = new CookieContainer (); co. setCookies (new Uri (server), cookie); httpWebRequest. cookieContainer = co; httpWebRequest. userAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon ;. net clr 1.1.4322) "; httpWebRequest. method = "GET"; webResponse = (HttpWebResponse) httpWebRequest. getResponse (); header = webResponse. headers. toString (); getStream = webResponse. getResponseStream (); streamReader = new StreamReader (getStream, Encoding. getEncoding ("gb2312"); getString = streamReader. readToEnd (); streamReader. close (); getStream. close (); return getString ;}}}

 


C # Use HttpWebRequest

Private bool AutoLogon (string userId, string password)
{Try
{
# Region obtain the logon FORM input box and Submit button
HtmlElement textboxUserId = this. webBrowser1.Document. GetElementById ("username ");
// If there is no ID, use Name to get it
// HtmlElement textboxUserId = this. webBrowser1.Document. All ["Name of the login username text box"];
HtmlElement textboxPassword = this. webBrowser1.Document. GetElementById ("password3 ");
// If there is no ID, use Name to get it
// HtmlElement textboxPassword = this. webBrowser1.Document. All ["Name in the logon Password box"];
// HtmlElement buttonSubmit = this. webBrowser1.Document. GetElementById ("Login button ID ");
// If there is no ID, use Name to get it
// HtmlElement buttonSubmit = this. webBrowser1.Document. All ["Name of the logon button"];
TextboxUserId. SetAttribute ("value", userId); // enter the account
TextboxPassword. SetAttribute ("value", password); // enter the password
// ButtonSubmit. InvokeMember ("click"); // trigger the click Event of the submit button
// Of course, you can log on or use
HtmlElement btn = this. webBrowser1.Document. GetElementById ("login ");
Btn. InvokeMember ("click ");
# Endregion
String html = this. webBrowser1.Document. Body. InnerHtml; ...... the remaining full text >>>
 
C # Use HttpWebRequest

CookieContainer cc = new CookieContainer ();
ASCIIEncoding encoding = new ASCIIEncoding ();
String postData = string. Empty;
PostData = "username =" + username; // For example, the name of the input box on the webpage is uname.
PostData + = "& password =" + pwd;
Byte [] data = encoding. GetBytes (postData );
HttpWebRequest myRequest = (HttpWebRequest) WebRequest. Create (""); // The logon page address.
MyRequest. Method = "POST ";
MyRequest. ContentType = "application/x-www-form-urlencoded ";
MyRequest. ContentLength = data. Length;
MyRequest. CookieContainer = cc;
MyRequest. KeepAlive = true;
Stream newStream = myRequest. GetRequestStream ();
NewStream. Write (data, 0, data. Length );
NewStream. Close ();

HttpWebResponse myResponse = (HttpWebResponse) myRequest. GetResponse (); // receives the returned value
StreamReader reader = new StreamReader (myResponse. GetResponseStream (), Encoding. Default );
String content = reader. ReadToEnd ();
 

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.