Httpclient 3.0 preliminary study

Source: Internet
Author: User
I have been trying to use httpclient for some automation work recently. But now it seems that it is not as easy as you think. Log on to a website, access a page with a specific feature, and publish an article Article Uploading some files is not a simple task.
The basic use of httpclient Code There are several examples, which are not difficult to grasp. Next, let me talk about some of my problems.

1. login verification image problems
First, if the website doesn't want you to program it when you log onUsually, a verification image is designed. This technology is so easy to use now.So that code can be directly used wherever you go. For image verification, there is still no error.

2. Dumb cookie problems:
If you want to send multiple cookies, you can actually send them as follows: State. addcookies ( New Cookie [] {
New Cookie ( " Www.aaa.com " , " Popped " , " Yes " , " / " , New Date ( 2006 , 12 , 8 ), False ),
New Cookie ( " Www.aaa.com " , " Rtime " , " 2 " , " / " , New Date ( 2006 , 12 , 8 ), False ),
New Cookie ( " Www.aaa.com " , " Ltime " , " 1149940477953 " , " / " , New Date ( 2006 , 12 , 8 ), False ),
New Cookie ( " Www.aaa.com " , " Cnzz02 " , " 1 " , " / " , New Date ( 2006 , 12 , 8 ), False ),
} );

But the packet capture will find that httpclient has many components in the header.Each item contains only one cookie., Which is different from IE. IE and Firefox will put all cookiE is packaged into one, and each item is separated by a semicolon in the cookie.There is a space in the middle.
Therefore, if httpclient is used, Use the following form:

String cookies =   " Yes; rtime = 2; ltime = 1149940477953; cnzz02 = 1 " ;
State. addcookie ( New Cookie ( " Blog.aaa.com " , " Poped " , Cookies, " / " , New Date ( 2006 , 12 , 8 ), False ));

3. Encoding Problems:

Httpclient is not as intelligent as IE or Firefox. Httpclient cannot remember the encoding of the previous session. So-8859-1, it will be in the content -The method specified in type is also very simple.
The original form is to be submitted. By default, the application/x -WWW-form-urlencoded to send If you send a packet in IE, you can see that such a CO is specified in the message. Ntent-type, but IE has compiled the packet content according to the session The Code has been converted. But httpclient doesn't work, and it doesn't know how to convert it. If you do not set the charset for conversion, it will follow the charse T = ISO-8859-1 converted. At this point, httpclient should It should be improved, because remember that the page code sent by the server is very simple. Events. You only need to save the status of headers such as "Content-Type: text/html; charset = UTF-8.
To make up for the performance of httpclient Set the parameters as follows:
Postmethod. addrequestheader ("Content-Type", "application/x -WWW-form-urlencoded; charset = UTF-8 ");
4. File Upload problem: httpclient cannot simulate File Upload well.
First, for the file type Ie can select different content according to the file type. -Type, and httpclient needs to be set by ourselves. In this way, if a directory is automatically uploaded, you must know all the files in the directory. And the corresponding charset, and then set the filepart Determine the file extension one by one to assign different content -Type and charset. For some web pages, the encoding is gb2312. While some other web pages are encoded with UTF-8, it will be more troublesome.
Second, if the multipartpost message is sent in addition to filepart, other input information in Form is required, which is a very troublesome task. At the beginning, I thought that postmethod. addparameters () could be used just like the postmethod. Later, I found that the multipartpost and the post are actually two different postmethods. Although form is used on the webpage, multipartpost only contains a file-type input in the input type. The others are exactly the same, but in fact there are two completely different solutions in httpclient. For multipartpost, you must use addparameters () instead of addparameters ().

somemultipartpost. setrequestentity (
New multipartrequestentity (
New part [] {art1, Part2, Part3, part4} ,
somemultipartpost. getparams ()
);

This form. For the file to be uploaded, these parts are filepart objects. If they are other inputs that follow the form, these parts are stringparts. The sent message is in the form of content-Disposition: Form-data; name = "newfoldername.

To sum up, we can't blame people for httpclient. There are so many things that are easy to do in the world, especially in the face of a simple protocol like HTTP.
In fact, there are already a lot of documents for such open-source things that have been open-source for a long time. Although they are sometimes not very easy to find, many questions can still be guessed or tried out. After all, they are relatively mature, and follow the RFC. As I asked a question on jellyfish, some people said, what can be simulated in the world like Ie? It cannot meet the requirements of Internet Explorer.
I feel like I want to use httpclient, or other similar tools (I don't know anything about other tools. Please let me know if I have any knowledge about such tools ), in fact, we still need to thoroughly understand the Protocol so that once we encounter any problems, we will know where the problem is. Otherwise, we will only be depressed.
I went to the Apache website yesterday and saw a new project called httpcomponent, which was removed from the common, including httpcore, httpclient, httpasync, httpnio, httpcookie, and httpconn, I don't know what it looks like, but httpcore 4.0 alpha2 has been released, and there may be new changes in httpclient later.

A week is not used much. please correct me if something is wrong.

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.