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
- Httpclient =NewHttpclient (); // create a client, similar to opening a browser
- Getmethod =NewGetmethod ("http://www.blablabla.com"); // create a get method, similar to entering an address in the browser address bar
- IntStatuscode=httpclient.exe cutemethod (getmethod); // press enter -- punch!
- System. Out. println ("response =" + getmethod. getresponsebodyasstring (); // you can view the fist hit and get many other things, such as head and cookies.
- Getmethod. releaseconnection (); // release. Remember to stop.
2. Support for HTTPS
How does one support HTTPS?
Java code
- Static{
- Protocol easyhttps =NewProtocol ("HTTPS ",NewEasysslprotocolsocketfactory (), 443 );
- Protocol. registerprotocol ("HTTPS", easyhttps );
- }
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
- Httpclient =NewHttpclient ();
- Httpclient. getparams (). setcookiepolicy (cookiepolicy. rfc_2109); // rfc_2109 is a widely supported cookie protocol.
- Httpstate initialstate =NewHttpstate ();
- Cookie =NewCookie ();
- Cookie. setdomain ("www.balblabla.com ");
- Cookie. setpath ("/");
- Cookie. setname ("Passionate environment ");
- Cookie. setvalue ("heartless ");
- Initialstate. addcookie (cookie );
- Httpclient. setstate (initialstate );
- ...
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
- ... // After some get/post methods are executed
- Cookie [] cookies = httpclient. getstate (). getcookies ();
- System. Out. println ("present cookies :");
- For(IntI = 0; I <cookies. length; I ++) {// cyclic structure parts
- System. Out. println ("-" + Cookies [I]. toexternalform ());
- System. Out. println ("-domain =" + Cookies [I]. getdomain ());
- System. Out. println ("-Path =" + Cookies [I]. getpath ());
- ...
- }
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
- Postmethod =NewPostmethod ("http://www.saybot.com/postme ");
- Namevaluepair [] postdata =NewNamevaluepair [2];
- Postdata [0] =NewNamevaluepair ("Weapon", "gun ");
- Postdata [1] =NewNamevaluepair ("What gun", "gun ");
- Postmethod. addparameters (postdata );
- ... // 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
- Httpclient =NewHttpclient ();
- Httpclient. gethostconfiguration (). setproxy ("192.168.0.1", 9527 );
- Httpclient. getparams (). setauthenticationpreemptive (True); // Important !!! Tell httpclient to use preemptive authentication. Otherwise, you will receive the consequence of "you are not qualified ".
- /*
- This step is also crucial. myproxycredentialsprovider implements the org. Apache. commons. httpclient. Auth. credentialsprovider interface,
- Returns the proxy's credential (username/password )*/
- Httpclient. getparams (). setparameter (credentialsprovider. provider,NewMyproxycredentialsprovider ());
- Httpclient. getstate (). setproxycredentials (
- NewAuthscope ("192.168.0.1 ",
- Authscope. any_port, // any port. Be careful.
- Authscope. any_realm), // any domain. Be careful.
- NewUsernamepasswordcredentials ("username", // The username of the proxy
- "Password"); // proxy Password