Use Apache httpclient

Source: Internet
Author: User

Commons-logging.jar and commons-codec.jar. The two bags don't have any error.

Httpclient 4.0 beta2

 

Code
Package com. testhttpclient;

Import java. Io. ioexception;
Import java. util. arraylist;
Import java. util. List;

Import org. Apache. http. httpentity;
Import org. Apache. http. httpresponse;
Import org. Apache. http. namevaluepair;
Import org. Apache. http. Client. httpclient;
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. Params. clientpnames;
Import org. Apache. http. Client. Params. cookiepolicy;
// Import org. Apache. http. Client. Params. httpclientparams;
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;

Public class maintest {
Public static void main (string [] ARGs ){
String url = "";
Httpclient = new defaulthttpclient ();
// Httpclient. getparams (). setparameter (httpclientparams. cookie_policy, cookiepolicy. browser_compatibility );
Httpclient. getparams (). setparameter (clientpnames. cookie_policy, cookiepolicy. browser_compatibility );

// Get Method
Httpget = new httpget (URL );
Httpget. setheader ("Accept-language", "ZH-CN "); //
Try {
Httpresponse response = httpclient.exe cute (httpget );
Httpentity entity = response. getentity ();
String webcode = new string (entityutils. tostring (response. getentity (). getbytes ("ISO-8859-1"), "UTF-8"); // detailed analysis
If (entity! = NULL ){
Try {
Entity. consumecontent ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
} Catch (exception ex ){
Ex. printstacktrace ();
}

// POST method
Httppost = new httppost (URL );
Httppost. setheader ("","");

// Namevaluepair [] nvps = new namevaluepair [] {
// New basicnamevaluepair ("",""),
// New basicnamevaluepair ("","")//
//};
// Httppost. setentity (New urlencodedformentity (nvps, HTTP. utf_8 ));
List <namevaluepair> parameters = new arraylist <namevaluepair> ();
Parameters. Add (New basicnamevaluepair ("",""));//

Try {
Httppost. setentity (New urlencodedformentity (parameters, HTTP. utf_8 ));
Httpresponse response = httpclient.exe cute (httppost );
Httpentity entity = response. getentity ();
String webcode = entityutils. tostring (response. getentity ());
If (entity! = NULL ){
Try {
Entity. consumecontent ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
} Catch (exception ex ){
Ex. printstacktrace ();
}
}
}

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.