Seven Apache common httpclient weapons

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 =NewHttpclient (); // create a client, similar to opening a browser
  2. Getmethod =NewGetmethod ("http://www.blablabla.com"); // create a get method, similar to entering an address in the browser address bar
  3. IntStatuscode=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 =NewProtocol ("HTTPS ",NewEasysslprotocolsocketfactory (), 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 are always concerned:

Java code
  1. Httpclient =NewHttpclient ();
  2. Httpclient. getparams (). setcookiepolicy (cookiepolicy. rfc_2109); // rfc_2109 is a widely supported cookie protocol.
  3. Httpstate initialstate =NewHttpstate ();
  4. Cookie =NewCookie ();
  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-emotional ring is the 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(IntI = 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 =NewPostmethod ("http://www.saybot.com/postme ");
  2. Namevaluepair [] postdata =NewNamevaluepair [2];
  3. Postdata [0] =NewNamevaluepair ("Weapon", "gun ");
  4. Postdata [1] =NewNamevaluepair ("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, everything is useless:

Java code
  1. Httpclient =NewHttpclient ();
  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,NewMyproxycredentialsprovider ());
  8. Httpclient. getstate (). setproxycredentials (
  9. NewAuthscope ("192.168.0.1 ",
  10. Authscope. any_port, // any port. Be careful.
  11. Authscope. any_realm), // any domain. Be careful.
  12. NewUsernamepasswordcredentials ("username", // The username of the proxy
  13. "Password"); // proxy Password

 

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.