HttpClient Post and Get visit webpage, httpclientpostget

Source: Internet
Author: User
Tags html header

HttpClient Post and Get visit webpage, httpclientpostget
1. Basic JAR package the jar package to be configured in pom. xml under Maven Project

<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.8.2</version><scope>test</scope></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient-cache</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpmime</artifactId><version>4.1.2</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.16</version></dependency></dependencies>

Ii. Source Code
Package com. qidou. httpclient; import java. io. unsupportedEncodingException; import java. util. arrayList; import java. util. iterator; import java. util. list; import java. util. map; import java. util. map. entry; import java. util. set; import org. apache. http. httpResponse; import org. apache. http. nameValuePair; import org. apache. http. client. entity. urlEncodedFormEntity; import org. apache. http. client. methods. httpGet; import Org. apache. http. client. methods. httpPost; import org. apache. http. client. methods. httpUriRequest; import org. apache. http. impl. client. defaultHttpClient; import org. apache. http. message. basicNameValuePair; import org. apache. http. protocol. HTTP; import org. apache. http. util. entityUtils; import org. apache. log4j. logger;/*** the HTTP request client provides two methods: GET and POST. ** @ Author Administrator **/public class HttpClient {/*** log object. */Private static Logger logger = Logger. getLogger (HttpClient. class);/*** default HTTP request client object. */Private DefaultHttpClient _ httpclient;/*** custom message header. */Private Map <String, String> _ headers;/*** use the default client object. */Public HttpClient () {// 1. Create an HttpClient object. _ Httpclient = new DefaultHttpClient (); logger.info ("create _ httpclient...") ;}/ *** the caller specifies the client object. ** @ Param httpclient */public HttpClient (Map <String, String> headers) {// 1. Create an HttpClient object. _ Httpclient = new DefaultHttpClient (); this. _ headers = headers; logger.info ("create _ httpclient...");}/*** http post request. ** @ Param url * @ param params * @ return * @ throws InterruptedException */public HttpResponse post (String url, Map <String, String> params) {// 2. create an instance of the Request Method, specify the request URL, and add request parameters. HttpPost post = postForm (url, params); logger.info ("create httppost:" + url); return invoke (post) ;}/ *** http get request. ** @ Param url * @ return */public HttpResponse get (String url) {HttpGet get = new HttpGet (url); logger.info ("create httpget:" + url ); return invoke (get);}/*** sends a request to process the response. * @ Param request * @ return */private HttpResponse invoke (HttpUriRequest request) {if (this. _ headers! = Null) {// addHeaders (request); logger.info ("addHeaders to http... ");} HttpResponse response = null; try {// 3. call the execute (HttpUriRequest request) of the HttpClient object to send a request and return an HttpResponse. Response = _httpclient.exe cute (request); logger.info ("execute http success ...; body = "+ EntityUtils. toString (response. getEntity ();} catch (Exception e) {e. printStackTrace (); logger.info ("execute http exception... ");} finally {// 4. the connection must be released no matter whether the execution method is successful or not. Request. abort (); logger.info ("release http...") ;}return response ;}/ *** get the post method. ** @ Param url * @ param params * @ return */private HttpPost postForm (String url, Map <String, String> params) {HttpPost httpost = new HttpPost (url ); list <NameValuePair> nvps = new ArrayList <NameValuePair> (); // assemble parameters. Set <String> keySet = params. keySet (); for (String key: keySet) {nvps. add (new BasicNameValuePair (key, params. get (key);} try {logger.info ("set UTF-8 form entity to httppost... "); httpost. setEntity (new UrlEncodedFormEntity (nvps, HTTP. UTF_8);} catch (UnsupportedEncodingException e) {e. printStackTrace ();} return httpost;}/*** adds a message header. ** @ Param httpost */private void addHeaders (HttpUriRequest httpost) {Iterator <Entry <String, String> it = this. _ headers. entrySet (). iterator (); Entry <String, String> entry = null; String name = null; String value = null; while (it. hasNext () {entry = it. next (); name = entry. getKey (); value = entry. getValue (); httpost. addHeader (name, value) ;}/ *** close the HTTP client link. */Public void shutdown () {_ httpclient. getConnectionManager (). shutdown (); logger.info ("shutdown _ httpclient ...");}}

Iii. test example
Package com. qidou. httpclient; import java. util. HashMap; import java. util. Map; import org. apache. http. HttpResponse;/*** HTTP client test class. */Public class TestHttpClient {/*** HTTP request client. */Private HttpClient httpClient;/*** initialize the HTTP request client. */{System. out. println ("init http client... "); Map <String, String> headers = new HashMap <String, String> (); headers. put ("Accept", "*/*"); headers. put ("Accept-Encoding", "gzip, deflate"); headers. put ("Accept-Language", "zh-cn"); headers. put ("Connection", "Keep-Alive"); headers. put ("Cookie", "UC_SID = 66134277449dcc94b77dfbcdb65a3d70; cna = HnmMDIROzW4CAbdDI + iUySCj; PHPSESSID = signature; lz Stat_uv = 260674130547154807 | 3037573; lzstat_ss = 582317835_2_1409680845_3037573; PHPSESSID = 918085719e575f59bd5f4b31df402f; frontvar = lns % 3Dmenu-1% 2Cflow-0% 2Ctraf-0% 2Ccont-0% percent % 2Ceanalysis-0% percent % 2C % 26 cmenu % 3Dsite_overview % 26 siteShowHis % 3 Dopen; CNZZDATA30086426 = cnzz_eid % %%253a % 252F % 252Ftongji.cnzz.com % 252F % 26 ntime % 3D1409650488; CNZZDATA30069868 = cnzz_eid % 3D24 %%253a % 252F % %%252f % 26 ntime % 3d14096%6; CNZZDATA33222 = cnzz_eid % %%253a % 252F % 252Ftongji.cnzz.com % 252F % 26 ntime % 3D1409651227; CNZZDATA30001831 = cnzz_eid % 3D732886644-1409644865-http % 253A % 252F % 252Ftongji.cnzz.com % 252F % 26 ntime % 3D1409649918; _ cnzz_CV30069868 = % E6 % 98% AF % E5 % 90% A6 % E7 % 99% BB % E5 % BD % 95% 7C % E6 % AD % A3 % E5 % B8 % B8 % E7 % 99% BB % E5 % BD % 95% 7C1409 680810339 "); headers. put ("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2 ;. net clr 2.0.50727 ;. net clr 3.5.30729 ;. net clr 3.0.30729) "); headers. put ("Referer", "http://www.baidu.com/"); headers. put ("x-requested-with", "XMLHttpRequest"); // create an HTTP request client. You can set the parameters of the message header: httpClient = new HttpClient (headers ); httpClient = new HttpClient ();}/*** test method. * Implementation process: * 1, use the user name/password login Renren Network: http://www.renren.com/PLogin.do * 2, access blog: http://blog.renren.com/blog/304317577/449470467 */@ org. junit. testpublic void testPost () {String url = "http://www.renren.com/PLogin.do"; String redirectURL = "http://blog.renren.com/blog/304317577/449470467"; String userName = ".... "; String password = ".... ";/*** Login parameters. */Map <String, String> params = new HashMap <String, String> (); params. put ("origURL", redirectURL); params. put ("domain", "renren.com"); params. put ("isplogin", "true"); params. put ("formName", ""); params. put ("method", ""); params. put ("submit", "login"); params. put ("email", userName); params. put ("password", password); // send a request to log on to Renren. HttpResponse postResponse = httpClient. post (url, params); // get the jump address of the message header and jump to the specified blog. String redirectLocation = postResponse. getFirstHeader ("Location"). getValue (); System. out. println (redirectLocation); // get the jump to the blog page. HttpClient. get (redirectLocation); // close the HTTP request client. HttpClient. shutdown ();}}

Iv. Running result init http client...
18:31:48 [com. qidou. httpclient. HttpClient]-[INFO] create _ httpclient...
18:31:48 [com. qidou. httpclient. HttpClient]-[INFO] set UTF-8 form entity to httppost...
18:31:48 [com. qidou. httpclient. HttpClient]-[INFO] create httppost: http://www.renren.com/PLogin.do
2014-09-04 18:31:49 [org. apache. http. client. protocol. responseProcessCookies]-[WARN] Cookie rejected: "[version: 0] [name: t] [value: Unknown] [domain: renren.com] [path:/xtalk/] [expiry: null] ". illegal path attribute "/xtalk /". path of origin: "/PLogin. do"
2014-09-04 18:31:49 [com. qidou. httpclient. httpClient]-[INFO] execute http success ...; body = The URL has moved <a href = "http://blog.renren.com/blog/304317577/449470467"> here </a>


18:31:49 [com. qidou. httpclient. HttpClient]-[INFO] release http...
Http://blog.renren.com/blog/304317577/449470467
18:31:49 [com. qidou. httpclient. HttpClient]-[INFO] create httpget: http://blog.renren.com/blog/304317577/449470467
18:31:49 [com. qidou. httpclient. HttpClient]-[INFO] execute http success...; body = the blog page is too large to be skipped...


18:31:49 [com. qidou. httpclient. HttpClient]-[INFO] release http...
18:31:49 [com. qidou. httpclient. HttpClient]-[INFO] shutdown _ httpclient...

What is the difference between the httpclient get method and the post method (web page deduction technology)

1. get is to get data from the server, and post is to send data to the server.
2. get is to add the parameter data queue to the URL referred to by the ACTION attribute of the submission form. The values correspond to each field in the form one by one and can be seen in the URL. Post uses the HTTP post mechanism to place fields in the form and their content in the html header and send them to the URL address referred to by the ACTION attribute. You cannot see this process.
3. For the get method, the server uses Request. QueryString to obtain the value of the variable. For the post method, the server uses Request. Form to obtain the submitted data.
4. The data volume transmitted by get is small and cannot exceed 2 kb. The amount of data transmitted by post is large, which is generally not restricted by default. Theoretically, the maximum size of IIS4 is 80 KB, and that of IIS5 is 100KB.
5. Low get security and high post security. However, the execution efficiency is better than the Post method.

Suggestion:
1. The get method is more secure than the Post method. If it contains confidential information, we recommend that you use the Post data submission method;
2. We recommend that you use the Get Method for Data Query. We recommend that you use the Post method when adding, modifying, or deleting data.

How to determine a useful URL, used in httpClient

What is the difference between URL and Post? A URL contains many post requests, such as the homepage of a portal, which contains many JS, text content, and images. Although only one URL is accessed, many resources will be post.

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.