Httpwebrequest and httpwebresponse

Source: Internet
Author: User

I have worked on a project to send text messages and send telephone voices. It involves applications of WebService and Windows service. I didn't think of any good way to debug Windows Service at the beginning, so I directly created a Windows application.ProgramThen, reference the Windows service project, set the breakpoint where the method in the Windows service project is called, and perform tracking. In a Windows Service Project, you must call another URL and PASS Parameters to update the database. httpwebrequest and httpwebresponse are used. Httpwebrequest and httpwebresponse will be used to create a project these days. I just want to sum up the relevance of these two classes. Today, I can't write any more. Let's try again in another day.

The following content is excerpted:

Httpwebrequest is a class in the. NET base class library. Under the namespace system. net, it is used to allow users to interact with the server through the HTTP protocol.

Httpwebrequest completely encapsulates the HTTP protocol and supports attributes and methods for headers, content, and cookies in the HTTP protocol, it is easy to compile a program that simulates automatic browser logon.

The program uses http protocol and server interaction mainly for data submission. Generally, data submission is done through get and post methods. The following two methods are described below:

1. Get method. Get method to complete data submission by attaching parameters in the network address, such as in the address http://www.google.com/webhp? In hl = ZH-CN, the previous part of the http://www.google.com/webhp 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 ). ProgramCodeAs follows:

Httpwebrequest Req = (httpwebrequest) httpwebrequest. Create ("http://www.google.com/webhp? Hl = ZH-CN ");
Req. method = "get ";
Using (webresponse wR = Req. getresponse ())
{
// Process the received page content here
}

2. 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:

String Param = "Hl = ZH-CN & newwindow = 1 ";
Byte [] BS = encoding. ASCII. getbytes (PARAM );

Httpwebrequest Req = (httpwebrequest) httpwebrequest. Create ("http://www.google.com/intl/zh-CN ");
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 above Code, we visited www.google.com, 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.

3. Use get to submit Chinese data. The get method adds parameters to the network address to complete data submission. For Chinese encoding, the commonly used two types are gb2312 and utf8. The program code accessed by gb2312 encoding is as follows:

Encoding myencoding = encoding. getencoding ("gb2312 ");
String address = "http://www.baidu.com/s? "+ 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.baidu.com/s 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. In common websites, www.baidu.com (Baidu) is encoded in gb2312, and www.google.com (Google) is encoded in utf8.

4. 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:

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.baidu.com/s ");
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.

The above lists the interactions between the client and the server using the HTTP protocol, commonly used get and post methods. The popular WebService also interacts with each other through the HTTP protocol, using the POST method. The difference is that the data content submitted by WebService and the received data content are all encoded in XML format. Therefore, httpwebrequest can also be used when WebService is called.

Link: http://www.cnblogs.com/webman/archive/2006/11/17/564106.html

Let's take a look at others. I'm tired of writing it myself!

Http://www.cnblogs.com/ashui/archive/2008/10/10/1308314.html

Http://www.cnblogs.com/ashui/archive/2008/10/10/1308314.html

Http://www.cnblogs.com/whzncut/archive/2008/11/20/1337851.html

Http://www.cnblogs.com/jmtek/archive/2006/02/20/334012.html

Http://www.cnblogs.com/yiki/archive/2007/08/28/872528.html

Http://www.cnblogs.com/allonkwok/archive/2006/12/12/589930.html

Http://www.cnblogs.com/teacher-dotnet/archive/2007/04/19/719154.html

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.