Simulate the get and post actions of the browser

Source: Internet
Author: User

Jakarta's httpclient3.1 is the latest version. In this project, you must use a program to simulate the get and post actions of the browser. Many problems are encountered during use.
1. Post submission with attachments
The class multipartpostmethod was used at the beginning and is now obsolete. API description:Deprecated. UseMultipartRequestEntityIn conjunctionPostMethodInstead.With postmethod, there is no need to create another multipartpostmethod. The following is a simple example:

Postmethod post = new postmethod ();
Namevaluepair [] pairs = new namevaluepair [2];
Pairs [0] = new namevaluepair ("para1", "value1 ");
Pairs [0] = new namevaluepair ("para2", "value2 ");
Post. setrequestbody (pairs );
Httpclient client = new httpclient ();
Try {
Client.exe cutemethod (post );
} Catch (httpexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
}

This is for normal form submission, and this form does not contain attachments. If an attachment is included, this method does not work. The attachment upload parameters and common parameters cannot be obtained together on the server. The Org. Apache. commons. httpclient. Methods. multipart package is used to process multiform parameters such as file upload. The main class is the part (representing a post object). It has two important subclasses: filepart and stringpart. One is the file parameter, and the other is the common text parameter. Its typical usage is as follows:

String url = "http: // localhost: 8080/httptest/test ";
Postmethod = new postmethod (URL );

Stringpart sp = new stringpart ("text", "testvalue ");
Filepart fp = new filepart ("file", "test.txt", new file ("./temp/test.txt "));

Multipartrequestentity MRP = new multipartrequestentity (new part [] {sp, FP}, postmethod
. Getparams ());
Postmethod. setrequestentity (MRP );

// Execute postmethod
Httpclient = new httpclient ();
Try {
Httpclient.exe cutemethod (postmethod );
} Catch (httpexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
}

In the second row postmethod = new postmethod (); later, someone said they needed to use postmehtodd. setRequestHeader ("Content-Type", "multipart/form-Data"); change the request type of Content-Type. However, I did not add this sentence during usage. I checked that the default Content-Type of httpcleint is application/octet-stream. There should be no impact. For mime-type requests, we recommend that you use mulitpartrequestentity to wrap httpclient.

2. Processing of parameters in Chinese
Httpclient default encoding is ISO-8859-1, it certainly cannot support Chinese parameters. Reference this article: http://thinkbase.net/w/main/Wiki? Httpclient + post + % E7 % 9A % 84 + UTF-8 + % E7 % BC % 96% E7 % A0 % 81% E9 % 97% AE % E9 % A2 % 98, according to the author, the problem of Chinese encoding can be solved normally. The most important thing is to modify a method implementation of the encodingutil class. In addition, the filepart and stringpart constructor methods both have a parameter with the specified encoding. To reduce the problem, we recommend that all the parameters be encoded in a uniform manner, including postmethod. getparams (). Example:

String url = "http: // localhost: 8080/httptest/test ";
Postmethod = new postmethod (URL );

Stringpart sp = new stringpart ("text", "testvalue", "gb2312 ");
Filepart fp = new filepart ("file", "test.txt", new file ("./temp/test.txt"), null, "gb2312 ");

Postmethod. getparams (). setcontentcharset ("gb2312 ");
Multipartrequestentity MRP = new multipartrequestentity (new part [] {sp, FP}, postmethod
. Getparams ());
Postmethod. setrequestentity (MRP );

// Execute postmethod
Httpclient = new httpclient ();
Try {
Httpclient.exe cutemethod (postmethod );
} Catch (httpexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
}

Simulate the get and post actions of the browser

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.