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 Use the Get method to read the page through the proxy server example.
*
* @author java Century web (java2000.net, laozizhu.com)
*/
public class Httpclientget {
public static void Main (string[] args) throws Exception {
Defaulthttpclient httpclient = new Defaulthttpclient ();
Access to the target site, port, and protocol
Httphost targethost = new Httphost ("www.java2000.net");
Settings for Agents
Httphost proxy = new Httphost ("10.60.8.20", 8080);
Httpclient.getparams (). Setparameter (Connroutepnames.default_proxy, PROXY);
Destination Address
HttpGet httpget = new HttpGet ("/");
System.out.println ("target:" + targethost);
SYSTEM.OUT.PRINTLN ("Request:" + httpget.getrequestline ());
Perform
HttpResponse response = Httpclient.execute (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 ());
}
Show results
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 Example of using post to submit common form data.
*
* @author java Century web (java2000.net, laozizhu.com)
*/
public class Httpclientpost {
public static void Main (string[] args) throws Exception {
Defaulthttpclient httpclient = new Defaulthttpclient ();
Settings for Agents
Httphost proxy = new Httphost ("10.60.8.20", 8080);
Httpclient.getparams (). Setparameter (Connroutepnames.default_proxy, PROXY);
Destination Address
HttpPost HttpPost = new HttpPost ("http://www.java2000.net/login.jsp");
SYSTEM.OUT.PRINTLN ("Request:" + httppost.getrequestline ());
Constructs the simplest string data
Stringentity reqentity = new Stringentity ("Username=test&password=test");
Set type
Reqentity.setcontenttype ("application/x-www-form-urlencoded");
Set the requested data
Httppost.setentity (reqentity);
Perform
HttpResponse response = Httpclient.execute (HttpPost);
httpentity entity = response.getentity ();
System.out.println ("----------------------------------------");
System.out.println (Response.getstatusline ());
if (Entity! = null) {
System.out.println ("Response Content Length:" + entity.getcontentlength ());
}
Show results
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, HttpClient 4.0 code example to access HTTPS via 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;
/**
* HttpClient 4.0 code example to access HTTPS via proxy.
*
* @author java Century web (java2000.net, laozizhu.com)
*/
public class Httpsproxyget {
public static void Main (string[] args) throws Exception {
Defaulthttpclient httpclient = new Defaulthttpclient ();
Certified Data
I write this here, please fill in according to the actual situation
Httpclient.getcredentialsprovider (). SetCredentials (New Authscope ("10.60.8.20", 8080),
New Usernamepasswordcredentials ("username", "password"));
Access to the target site, port, and protocol
Httphost targethost = new Httphost ("www.google.com", 443, "https");
Settings for Agents
Httphost proxy = new Httphost ("10.60.8.20", 8080);
Httpclient.getparams (). Setparameter (Connroutepnames.default_proxy, PROXY);
Destination Address
HttpGet httpget = new HttpGet ("/adsense/login/zh_cn/?");
System.out.println ("target:" + targethost);
SYSTEM.OUT.PRINTLN ("Request:" + httpget.getrequestline ());
System.out.println ("Agent:" + proxy);
Perform
HttpResponse response = Httpclient.execute (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 ());
}
Show results
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, HttpClient read the use of the page example
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 Read page
* @author Lao Zi (java2000.net)
*
*/
public class HttpGet {
public static void Main (string[] args) throws Exception {
Httpparams params = new Basichttpparams ();
Version of the HTTP protocol, 1.1/1.0/0.9
Httpprotocolparams.setversion (params, httpversion.http_1_1);
Character
Httpprotocolparams.setcontentcharset (params, "UTF-8");
Type of browser masquerading
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, "httpcomponents/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.execute (Request, conn, context);
Response.setparams (params);
Httpexecutor.postprocess (response, Httpproc, context);
Return code
System.out.println ("<< Response:" + response.getstatusline ());
File header information returned
Header[] hs = Response.getallheaders ();
for (Header h:hs) {
System.out.println (H.getname () + ":" + h.getvalue ());
}
Output body 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
HttpClient uses iso-8859-1 to read the contents of the HTTP response by default, and if the content contains Chinese characters, it has to use the ugly new String (Str.getbytes ("iso-8859-1"), "GBK", and the statement.
The workaround is to use the following configuration.
private static final String Content_charset = "GBK";//httpclient character set used when reading content
HttpClient client = new HttpClient ();
Client.getparams (). Setparameter (Httpmethodparams.http_content_charset, Content_charset);
6, HttpClient 4 processing File Upload Example (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;
/**
* HttpClient 4 Processing File Upload Example (multipartentity) .<br>
* Requires James mime4j 0.5 version, 0.6 is not.
*
* @author java Century web (java2000.net, laozizhu.com)
*/
public class Httpclientmultipartformpost {
public static void Main (string[] args) throws Exception {
HttpClient HttpClient = new Defaulthttpclient ();
HttpPost 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 entities
Multipartentity reqentity = new multipartentity ();
Increase
Reqentity.addpart ("Bin", bin);
Reqentity.addpart ("comment", comment);
Set up
Httppost.setentity (reqentity);
System.out.println ("Execute:" + httppost.getrequestline ());
HttpResponse response = Httpclient.execute (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 ();
}
}
}
httpclient example of using get mode to read pages through a proxy server