HttpClient 4 使用方法的几个例子(代理,StringEntity字符串数据,文件上传)(转载)
1,HttpClient读取页面的使用例子:
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; public class HttpGet { public static void main(String[] args) throws Exception { HttpParams params = new BasicHttpParams(); // HTTP 协议的版本,1.1/1.0/0.9 HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); // 字符集 HttpProtocolParams.setContentCharset(params, "UTF-8"); // 伪装的浏览器类型 // IE7 是 // 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); // 返回码 System.out.println("<< Response: " + response.getStatusLine()); // 返回的文件头信息 Header[] hs = response.getAllHeaders(); for (Header h : hs) { System.out.println(h.getName() + ":" + h.getValue()); } // 输出主体信息 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(); } } }
2 ,HttpClient 4.0通过代理访问Https的代码例子:
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; public class HttpsProxyGet { public static void main(String[] args) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); // 认证的数据 // 我这里是瞎写的,请根据实际情况填写 httpclient.getCredentialsProvider().setCredentials(new AuthScope("10.60.8.20", 8080), new UsernamePasswordCredentials("username", "password")); // 访问的目标站点,端口和协议 HttpHost targetHost = new HttpHost("www.google.com", 443, "https"); // 代理的设置 HttpHost proxy = new HttpHost("10.60.8.20", 8080); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); // 目标地址 HttpGet httpget = new HttpGet("/adsense/login/zh_CN/?"); System.out.println("目标: " + targetHost); System.out.println("请求: " + httpget.getRequestLine()); System.out.println("代理: " + proxy); // 执行 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()); } // 显示结果 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使用GET方式通过代理服务器读取页面的例子:
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; public class HttpClientGet { public static void main(String[] args) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); // 访问的目标站点,端口和协议 HttpHost targetHost = new HttpHost("www.java2000.net"); // 代理的设置 HttpHost proxy = new HttpHost("10.60.8.20", 8080); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); // 目标地址 HttpGet httpget = new HttpGet("/"); System.out.println("目标: " + targetHost); System.out.println("请求: " + httpget.getRequestLine()); // 执行 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()); } // 显示结果 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 4 使用POST方式提交普通表单数据的例子:
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; public class HttpClientPost { public static void main(String[] args) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); // 代理的设置 HttpHost proxy = new HttpHost("10.60.8.20", 8080); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); // 目标地址 HttpPost httppost = new HttpPost("http://www.java2000.net/login.jsp"); System.out.println("请求: " + httppost.getRequestLine()); // 构造最简单的字符串数据 StringEntity reqEntity = new StringEntity("username=test&password=test"); // 设置类型 reqEntity.setContentType("application/x-www-form-urlencoded"); // 设置请求的数据 httppost.setEntity(reqEntity); // 执行 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()); } // 显示结果 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(); } } }
5,HttpClient 4处理文件上传的例子(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; public class HttpClientMultipartFormPost { public static void main(String[] args) throws Exception { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://localhost"); // 一个本地的文件 FileBody bin = new FileBody(new File("d:/BIMG1181.JPG")); // 一个字符串 StringBody comment = new StringBody("A binary file of some kind"); // 多部分的实体 MultipartEntity reqEntity = new MultipartEntity(); // 增加 reqEntity.addPart("bin", bin); reqEntity.addPart("comment", comment); // 设置 httppost.setEntity(reqEntity); System.out.println("执行: " + 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("返回长度: " + resEntity.getContentLength()); } if (resEntity != null) { resEntity.consumeContent(); } } }
6,httpclient中文乱码解决:
httpclient默认使用ISO-8859-1读取http响应的内容,如果内容中包含汉字的话就得动用丑陋的new String(str.getBytes("ISO-8859-1"),"GBK");语句了。
解决办法就是使用以下配置。
private static final String CONTENT_CHARSET = "GBK";// httpclient读取内容时使用的字符集 HttpClient client = new HttpClient(); client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, CONTENT_CHARSET)