Example of httpclient using the get method to read pages through the Proxy Server

Source: Internet
Author: User

Import java. Io. bufferedreader;
Import java. Io. inputstreamreader;
Import org. Apache. http. httpentity;
Import org. Apache. http. httphost;
Import org. Apache. http. httpresponse;
Import org. Apache. http. Client. Methods. httpget;
Import org. Apache. http. Conn. Params. connroutepnames;
Import org. Apache. http. impl. Client. defaulthttpclient;

/**
* Httpclient uses the get method to read pages through the proxy server.
*
* @ Author Java century network (java2000.net, laozizhu.com)
*/
Public class httpclientget {
Public static void main (string [] ARGs) throws exception {
Defaulthttpclient httpclient = new defaulthttpclient ();
// Target site, port, and Protocol
Httphost targethost = new httphost ("www.java2000.net ");
// Proxy settings
Httphost proxy = new httphost ("10.60.8.20", 8080 );
Httpclient. getparams (). setparameter (connroutepnames. default_proxy, proxy );
// Target address
Httpget = new httpget ("/");
System. Out. println ("target:" + targethost );
System. Out. println ("Request:" + httpget. getrequestline ());
// Execute
Httpresponse response = httpclient.exe cute (targethost, httpget );
Httpentity entity = response. getentity ();
System. Out. println ("----------------------------------------");
System. Out. println (response. getstatusline ());
If (entity! = NULL ){
System. Out. println ("response Content Length:" + entity. getcontentlength ());
}
// Display the result
Bufferedreader reader = new bufferedreader (New inputstreamreader (entity. getcontent (), "UTF-8 "));
String line = NULL;
While (line = reader. Readline ())! = NULL ){
System. Out. println (line );
}
If (entity! = NULL ){
Entity. consumecontent ();
}
}
}

 

2. httpclient 4. Example of using post to submit common form data

Import java. Io. bufferedreader;
Import java. Io. inputstreamreader;
Import org. Apache. http. httpentity;
Import org. Apache. http. httphost;
Import org. Apache. http. httpresponse;
Import org. Apache. http. Client. Methods. httppost;
Import org. Apache. http. Conn. Params. connroutepnames;
Import org. Apache. http. entity. stringentity;
Import org. Apache. http. impl. Client. defaulthttpclient;

/**
* Httpclient 4 uses the POST method to submit common form data.
*
* @ Author Java century network (java2000.net, laozizhu.com)
*/
Public class httpclientpost {
Public static void main (string [] ARGs) throws exception {
Defaulthttpclient httpclient = new defaulthttpclient ();
// Proxy settings
Httphost proxy = new httphost ("10.60.8.20", 8080 );
Httpclient. getparams (). setparameter (connroutepnames. default_proxy, proxy );
// Target address
Httppost = new httppost ("http://www.java2000.net/login.jsp ");
System. Out. println ("Request:" + httppost. getrequestline ());
// Construct the simplest string data
Stringentity reqentity = new stringentity ("username = Test & Password = test ");
// Set the type
Reqentity. setcontenttype ("application/X-WWW-form-urlencoded ");
// Set the requested data
Httppost. setentity (reqentity );
// Execute
Httpresponse response = httpclient.exe cute (httppost );
Httpentity entity = response. getentity ();
System. Out. println ("----------------------------------------");
System. Out. println (response. getstatusline ());
If (entity! = NULL ){
System. Out. println ("response Content Length:" + entity. getcontentlength ());
}
// Display the result
Bufferedreader reader = new bufferedreader (New inputstreamreader (entity. getcontent (), "UTF-8 "));
String line = NULL;
While (line = reader. Readline ())! = NULL ){
System. Out. println (line );
}
If (entity! = NULL ){
Entity. consumecontent ();
}
}
}

 

3. code example of httpclient 4.0 accessing HTTPS through proxy

Import java. Io. bufferedreader;
Import java. Io. inputstreamreader;
Import org. Apache. http. httpentity;
Import org. Apache. http. httphost;
Import org. Apache. http. httpresponse;
Import org. Apache. http. Auth. authscope;
Import org. Apache. http. Auth. usernamepasswordcredentials;
Import org. Apache. http. Client. Methods. httpget;
Import org. Apache. http. Conn. Params. connroutepnames;
Import org. Apache. http. impl. Client. defaulthttpclient;

/**
* Code example of httpclient 4.0 accessing HTTPS through proxy.
*
* @ Author Java century network (java2000.net, laozizhu.com)
*/
Public class httpsproxyget {
Public static void main (string [] ARGs) throws exception {
Defaulthttpclient httpclient = new defaulthttpclient ();
// Authentication data
// I am blind here, please fill in according to the actual situation
Httpclient. getcredentialsprovider (). setcredentials (New authscope ("10.60.8.20", 8080 ),
New usernamepasswordcredentials ("username", "password "));
// Target site, port, and Protocol
Httphost targethost = new httphost ("www.google.com", 443, "HTTPS ");
// Proxy settings
Httphost proxy = new httphost ("10.60.8.20", 8080 );
Httpclient. getparams (). setparameter (connroutepnames. default_proxy, proxy );
// Target address
Httpget = new httpget ("/AdSense/login/zh_cn /? ");
System. Out. println ("target:" + targethost );
System. Out. println ("Request:" + httpget. getrequestline ());
System. Out. println ("Proxy:" + proxy );
// Execute
Httpresponse response = httpclient.exe cute (targethost, httpget );
Httpentity entity = response. getentity ();
System. Out. println ("----------------------------------------");
System. Out. println (response. getstatusline ());
If (entity! = NULL ){
System. Out. println ("response Content Length:" + entity. getcontentlength ());
}
// Display the result
Bufferedreader reader = new bufferedreader (New inputstreamreader (entity. getcontent (), "UTF-8 "));
String line = NULL;
While (line = reader. Readline ())! = NULL ){
System. Out. println (line );
}
If (entity! = NULL ){
Entity. consumecontent ();
}
}
}

 

4. Example of using httpclient to read a page

Package com. laozizhu. Apache. httpclient;

Import java.net. Socket;

Import org. Apache. http. connectionreusestrategy;
Import org. Apache. http. header;
Import org. Apache. http. httphost;
Import org. Apache. http. httpresponse;
Import org. Apache. http. httpversion;
Import org. Apache. http. impl. defaultconnectionreusestrategy;
Import org. Apache. http. impl. defaulthttpclientconnection;
Import org. Apache. http. Message. basichttprequest;
Import org. Apache. http. Params. basichttpparams;
Import org. Apache. http. Params. httpparams;
Import org. Apache. http. Params. httpprotocolparams;
Import org. Apache. http. Protocol. basichttpcontext;
Import org. Apache. http. Protocol. basichttpprocessor;
Import org. Apache. http. Protocol. executioncontext;
Import org. Apache. http. Protocol. httpcontext;
Import org. Apache. http. Protocol. httprequestexecutor;
Import org. Apache. http. Protocol. requestconncontrol;
Import org. Apache. http. Protocol. requestcontent;
Import org. Apache. http. Protocol. requestexpectcontinue;
Import org. Apache. http. Protocol. requesttargethost;
Import org. Apache. http. Protocol. requestuseragent;
Import org. Apache. http. util. entityutils;

/**
* Example of using httpclient to read a page
* @ Author old zizhu (java2000.net)
*
*/
Public class httpget {
Public static void main (string [] ARGs) throws exception {

Httpparams Params = new basichttpparams ();
// HTTP Protocol version, 1.1/1.0/0.9
Httpprotocolparams. setversion (Params, httpversion. http_1_1 );
// Character Set
Httpprotocolparams. setcontentcharset (Params, "UTF-8 ");
// Camouflage browser type
// IE7 is
// Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)
//
// Firefox3.03
// Mozilla/5.0 (windows; U; Windows NT 5.2; ZH-CN; RV: 1.9.0.3) Gecko/2008092417 Firefox/3.0.3
//
Httpprotocolparams. setuseragent (Params, "http: // 1.1 ");
Httpprotocolparams. setuseexpectcontinue (Params, true );

Basichttpprocessor httpproc = new basichttpprocessor ();

Httpproc. addinterceptor (New requestcontent ());
Httpproc. addinterceptor (New requesttargethost ());

Httpproc. addinterceptor (New requestconncontrol ());
Httpproc. addinterceptor (New requestuseragent ());
Httpproc. addinterceptor (New requestexpectcontinue ());

Httprequestexecutor httpexecutor = new httprequestexecutor ();

Httpcontext context = new basichttpcontext (null );
Httphost host = new httphost ("www.java2000.net", 80 );

Defaulthttpclientconnection conn = new defaulthttpclientconnection ();
Connectionreusestrategy connstrategy = new defaultconnectionreusestrategy ();

Context. setattribute (executioncontext. http_connection, Conn );
Context. setattribute (executioncontext. http_target_host, host );

Try {

String [] targets = {"/", "/help. jsp "};

For (INT I = 0; I <targets. length; I ++ ){
If (! Conn. isopen ()){
Socket socket = new socket (host. gethostname (), host. getport ());
Conn. BIND (socket, Params );
}
Basichttprequest request = new basichttprequest ("get", targets [I]);
System. Out. println ("> request URI:" + request. getrequestline (). geturi ());

Context. setattribute (executioncontext. http_request, request );
Request. setparams (Params );
Httpexecutor. preprocess (request, httpproc, context );
Httpresponse response = httpexecutor.exe cute (request, Conn, context );
Response. setparams (Params );
Httpexecutor. postprocess (response, httpproc, context );

// Return code
System. Out. println ("<response:" + response. getstatusline ());
// Returned file header information
Header [] HS = response. getallheaders ();
For (header H: HS ){
System. Out. println (H. getname () + ":" + H. getvalue ());
}
// Output the subject information
System. Out. println (entityutils. tostring (response. getentity ()));
System. Out. println ("================ ");
If (! Connstrategy. keepalive (response, context )){
Conn. Close ();
} Else {
System. Out. println ("connection kept alive ...");
}
}
} Finally {
Conn. Close ();
}
}
}

5. httpclient Chinese garbled Solution

By default, httpclient uses a ISO-8859-1 to read the content of an HTTP response. If the content contains Chinese characters, you have to use the ugly new string (Str. getbytes ("ISO-8859-1"), "GBK"); statement.

The solution is to use the following configuration.

Private Static final string content_charset = "GBK"; // character set used to read content from httpclient

Httpclient client = new httpclient ();
Client. getparams (). setparameter (httpmethodparams. http_content_charset, content_charset );

 

 

6. Example of httpclient 4 processing File Upload (multipartentity)

 

Import java. Io. file;
Import org. Apache. http. httpentity;
Import org. Apache. http. httpresponse;
Import org. Apache. http. Client. httpclient;
Import org. Apache. http. Client. Methods. httppost;
Import org. Apache. http. entity. Mime. multipartentity;
Import org. Apache. http. entity. Mime. content. filebody;
Import org. Apache. http. entity. Mime. content. stringbody;
Import org. Apache. http. impl. Client. defaulthttpclient;

/**
* Example of httpclient 4 processing File Upload (multipartentity). <br>
* The version of James mime4j 0.5 is required.
*
* @ Author Java century network (java2000.net, laozizhu.com)
*/
Public class httpclientmultipartformpost {
Public static void main (string [] ARGs) throws exception {
Httpclient = new defaulthttpclient ();
Httppost = new httppost ("http: // localhost ");
// A local file
Filebody bin = new filebody (new file ("D:/bimg1181.jpg "));
// A string
Stringbody comment = new stringbody ("a binary file of some kind ");
// Multi-part Entity
Multipartentity reqentity = new multipartentity ();
// Add
Reqentity. addpart ("bin", BIN );
Reqentity. addpart ("comment", comment );
// Set
Httppost. setentity (reqentity );
System. Out. println ("execution:" + httppost. getrequestline ());
Httpresponse response = httpclient.exe cute (httppost );
Httpentity resentity = response. getentity ();
System. Out. println ("----------------------------------------");
System. Out. println (response. getstatusline ());
If (resentity! = NULL ){
System. Out. println ("Return length:" + resentity. getcontentlength ());
}
If (resentity! = NULL ){
Resentity. consumecontent ();
}
}
}

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.