Seven weapons used by Apache common httpclient

Source: Internet
Author: User

1. Fist get/post
Fist is the most basic weapon and the most important thing. For example, a martial artist must take a solid horse.

Java code
  1. Httpclient = new httpclient (); // create a client, similar to opening a browser
  2. Getmethod = new getmethod ("http://www.blablabla.com"); // create a get method, similar to entering an address in the browser address bar
  3. Int statuscode=httpclient.exe cutemethod (getmethod); // press enter -- punch!
  4. System. Out. println ("response =" + getmethod. getresponsebodyasstring (); // you can view the fist hit and get many other things, such as head and cookies.
  5. Getmethod. releaseconnection (); // release. Remember to stop.

2. Support for HTTPS

How does one support HTTPS? Java code

  1. Static {
  2. Protocol easyhttps = new protocol ("HTTPS", new easysslprotocolsocketfactory (), 443 );
  3. Protocol. registerprotocol ("HTTPS", easyhttps );
  4. }

Before you execute a specific HTTP method, register the HTTPS protocol, for example, peacock lingers, and conceal the secret. Remember, the official binary release does not have an SSL contribute package. The first method is to download the source code version to create your peacock.

3. Cookies
The common saying is that there are too many memories, so it's just like cookies. No matter where you go, you will always worry about it: Java code.

  1. Httpclient = new httpclient ();
  2. Httpclient. getparams (). setcookiepolicy (cookiepolicy. rfc_2109); // rfc_2109 is a widely supported cookie protocol.
  3. Httpstate initialstate = new httpstate ();
  4. Cookie = new cookie ();
  5. Cookie. setdomain ("www.balblabla.com ");
  6. Cookie. setpath ("/");
  7. Cookie. setname ("Passionate environment ");
  8. Cookie. setvalue ("heartless ");
  9. Initialstate. addcookie (cookie );
  10. Httpclient. setstate (initialstate );
  11. ...


4. Deconstruct cookies

The opposite side of the multi-cycle environment is a different hook, hook out, the enemy's body is a part of the body to be separated from the body, the end is heartless: Java code

  1. ... // After some get/post methods are executed
  2. Cookie [] cookies = httpclient. getstate (). getcookies ();
  3. System. Out. println ("present cookies :");
  4. For (INT I = 0; I <cookies. length; I ++) {// cyclic structure parts
  5. System. Out. println ("-" + Cookies [I]. toexternalform ());
  6. System. Out. println ("-domain =" + Cookies [I]. getdomain ());
  7. System. Out. println ("-Path =" + Cookies [I]. getpath ());
  8. ...
  9. }

5. overlord post Parameters
Gun, the king of long weapons, and many famous guns named after gold or silver, such as Yue Fei. When the POST method submits a parameter pair, it is like a spirit snake: Java code

  1. Postmethod = new postmethod ("http://www.saybot.com/postme ");
  2. Namevaluepair [] postdata = new namevaluepair [2];
  3. Postdata [0] = new namevaluepair ("Weapon", "gun ");
  4. Postdata [1] = new namevaluepair ("What gun", "gun ");
  5. Postmethod. addparameters (postdata );
  6. ... // Let's take a shot.


6. Proxy)

Proxy is very important, especially in the years when the lan went viral. Without proxy, you couldn't get QQ in the company, didn't have a proxy, and Google couldn't get Web snapshot, the prestige of proxy, comparable to the seven-star Jasper knife, no knife, in Lan and development, nothing goes wrong: Java code

  1. Httpclient = new httpclient ();
  2. Httpclient. gethostconfiguration (). setproxy ("192.168.0.1", 9527 );
  3. Httpclient. getparams (). setauthenticationpreemptive (true); // important !!! Tell httpclient to use preemptive authentication. Otherwise, you will receive the consequence of "you are not qualified ".
  4. /*
  5. This step is also crucial. myproxycredentialsprovider implements the org. Apache. commons. httpclient. Auth. credentialsprovider interface,
  6. Returns the proxy's credential (username/password )*/
  7. Httpclient. getparams (). setparameter (credentialsprovider. provider, new myproxycredentialsprovider ());
  8. Httpclient. getstate (). setproxycredentials (
  9. New authscope ("192.168.0.1 ",
  10. Authscope. any_port, // any port. Be careful.
  11. Authscope. any_realm), // any domain. Be careful.
  12. New usernamepasswordcredentials ("username", // proxy Username
  13. "Password"); // proxy Password

...

7. The world's first
Please refer to the official httpclient documentation: jakarta.apache.org/commons/httpclient/userguide.html.
Read notes written by Hilton: www.cnjm.net/tech/article1155.html
After reading this article, you will be able to feel overwhelmed.

Seven Weapons, finishing work!

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.