原文链接:http://huangrongyou.iteye.com/blog/1748722
简要介绍
HttpComponents 包括 HttpCore包和HttpClient包
HttpClient:Http的执行http请求
DefaultHttpClient:httpClient默认实现
HttpGet、HttpPost:Get、Post方法执行类
HttpResponse:执行返回的Response,含http的header和执行结果实体Entity
HttpEntity:Http返回结果实体,不含Header内容
HttpParam:连接参数,配合连接池使用
PoolingClientConnectionManager:连接池
基础Get方法
-
- HttpClient client = new DefaultHttpClient();
-
- HttpGet get = new HttpGet(url);
-
- HttpResponse response = client.execute(get);
-
- HttpEntity entity = response.getEntity();
- if (entity != null) {
- System.out.println( entity.getContentEncoding());
- System.out.println( entity.getContentType());
-
- InputStream instream = entity.getContent();
- BufferedReader reader = new BufferedReader(new InputStreamReader(instream, encoding));
- System.out.println(reader.readLine());
-
-
- }
-
-
- client.getConnectionManager().shutdown();
基础Post方法
- DefaultHttpClient httpclient = new DefaultHttpClient();
- HttpPost httpost = new HttpPost(url);
-
- List<NameValuePair> formparams = new ArrayList<NameValuePair>();
- formparams.add(new BasicNameValuePair("p", "1"));
- formparams.add(new BasicNameValuePair("t", "2"));
- formparams.add(new BasicNameValuePair("e", "3"));
-
- UrlEncodedFormEntity urlEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
- httpost.setEntity(urlEntity);
-
- HttpResponse response = httpclient.execute(httpost);
- HttpEntity entity = response.getEntity();
-
- System.out.println("Login form get: " + response.getStatusLine() + entity.getContent());
-
- System.out.println("Post logon cookies:");
- List<Cookie> cookies = httpclient.getCookieStore().getCookies();
- for (int i = 0; i < cookies.size(); i++) {
- System.out.println("- " + cookies.get(i).toString());
- }
-
- httpclient.getConnectionManager().shutdown();
保留Session,保留用户+密码状态
Demo1,只支持单线程
- DefaultHttpClient httpclient = new DefaultHttpClient(
- new ThreadSafeClientConnManager());
-
- HttpPost httpost = new HttpPost(url);
-
- List<NameValuePair> formparams = new ArrayList<NameValuePair>();
- formparams.add(new BasicNameValuePair("p", "1"));
- formparams.add(new BasicNameValuePair("t", "2"));
- formparams.add(new BasicNameValuePair("e", "3"));
-
- httpost.setEntity(new UrlEncodedFormEntity(formparams, Consts.UTF_8));
-
- httpclient.execute(httpost);
-
- httpost = new HttpPost(url2);
- BasicResponseHandler responseHandler = new BasicResponseHandler();
- System.out.println(httpclient.execute(httpost, responseHandler));
- httpclient.getConnectionManager().shutdown();
-
- return "";
Demo2:第二次请求带上第一次请求的Cookie
用于在用户+密码等候后,后续根据第一次请求的URL获取的Cookie,把这些Cookie添加到第二次请求的Cookie中
- DefaultHttpClient httpclient = new DefaultHttpClient();
- HttpPost httpost = new HttpPost(url);
-
- List<NameValuePair> formparams = new ArrayList<NameValuePair>();
- formparams.add(new BasicNameValuePair("uname", name));
- formparams.add(new BasicNameValuePair("pass", "e0c10f451217b93f76c2654b2b729b85"));
- formparams.add(new BasicNameValuePair("auto_login","0"));
- formparams.add(new BasicNameValuePair("a","1"));
- formparams.add(new BasicNameValuePair("backurl","1"));
-
- UrlEncodedFormEntity urlEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
- httpost.setEntity(urlEntity);
- HttpContext localContext = new BasicHttpContext();
-
- HttpResponse response = httpclient.execute(httpost,localContext);
- HttpEntity entity = response.getEntity();
-
- System.out.println(Arrays.toString(response.getAllHeaders()));
- System.out.println(EntityUtils.toString(entity));
-
-
- DefaultHttpClient httpclient2 = new DefaultHttpClient();
- HttpPost httpost2 = new HttpPost("http://my.ifeng.com/?_c=index&_a=my");
-
- CookieStore cookieStore2 = httpclient2.getCookieStore();
-
- CookieStore cookieStore = httpclient.getCookieStore();
- List<Cookie> list = cookieStore.getCookies();
- for(Cookie o : list){
- System.out.println(o.getName() + " = " + o.getValue() + " 12");;
- cookieStore2.addCookie(o);
- }
-
- HttpResponse response2 = httpclient2.execute(httpost2);
- HttpEntity entity2 = response2.getEntity();
- System.out.println(Arrays.toString(response2.getAllHeaders()));
- System.out.println(EntityUtils.toString(entity2));
获取访问上下文:
- HttpClient httpclient = new DefaultHttpClient();
-
- HttpGet get = new HttpGet(url);
- HttpContext localContext = new BasicHttpContext();
-
- httpclient.execute(get, localContext);
-
-
- HttpConnection con = (HttpConnection) localContext
- .getAttribute(ExecutionContext.HTTP_CONNECTION);
- System.out.println("socket超时时间:" + con.getSocketTimeout());
-
-
- HttpHost target = (HttpHost) localContext
- .getAttribute(ExecutionContext.HTTP_TARGET_HOST);
- System.out.println("最终请求的目标:" + target.getHostName() + ":"
- + target.getPort());
-
-
- HttpHost proxy = (HttpHost) localContext
- .getAttribute(ExecutionContext.HTTP_PROXY_HOST);
- if (proxy != null)
- System.out.println("代理主机的目标:" + proxy.getHostName() + ":"
- + proxy.getPort());
-
- System.out.println("是否发送完毕:"
- + localContext.getAttribute(ExecutionContext.HTTP_REQ_SENT));
-
-
- HttpRequest request = (HttpRequest) localContext
- .getAttribute(ExecutionContext.HTTP_REQUEST);
- System.out.println("请求的版本:" + request.getProtocolVersion());
- Header[] headers = request.getAllHeaders();
- System.out.println("请求的头信息: ");
- for (Header h : headers) {
- System.out.println(h.getName() + "--" + h.getValue());
- }
- System.out.println("请求的链接:" + request.getRequestLine().getUri());
-
-
- HttpResponse response = (HttpResponse) localContext
- .getAttribute(ExecutionContext.HTTP_RESPONSE);
- HttpEntity entity = response.getEntity();
- if (entity != null) {
- System.out.println("返回结果内容编码是:" + entity.getContentEncoding());
- System.out.println("返回结果内容类型是:" + entity.getContentType());
- }
连接池和代理:
每次使用最后一句new DefaultHttpClient(cm, httpParams);获取新的HttpClient
里面还有一条如何设置代理
-
- HttpParams httpParams = new BasicHttpParams();
-
-
- HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
-
- HttpConnectionParams.setSoTimeout(httpParams, 60000);
-
- SchemeRegistry schemeRegistry = new SchemeRegistry();
- schemeRegistry.register(
- new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
-
-
- PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
-
- cm.setMaxTotal(200);
-
- cm.setDefaultMaxPerRoute(20);
-
-
-
-
- HttpHost proxy = new HttpHost("10.36.24.3", 60001);
- httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
-
- HttpClient httpClient = new DefaultHttpClient(cm, httpParams);
自动重连
如果某次请求请求失败,可以自动重连
- DefaultHttpClient httpClient = new DefaultHttpClient();
-
- HttpRequestRetryHandler requestRetryHandler2 = new HttpRequestRetryHandler() {
-
- public synchronized boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
-
- if (executionCount > 3) {
-
- return false;
- }
- if (exception instanceof NoHttpResponseException) {
-
- return true;
- }
- if (exception instanceof SSLHandshakeException) {
-
- return false;
- }
- HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
- boolean idempotent = (request instanceof HttpEntityEnclosingRequest);
- if (!idempotent) {
-
- return true;
- }
- return false;
- }
- };
- httpClient.setHttpRequestRetryHandler(requestRetryHandler2);
使用自定义ResponseHandler处理返回的请求
- HttpClient httpClient = new DefaultHttpClient();
- HttpGet get = new HttpGet(url);
-
- ResponseHandler<byte[]> handler = new ResponseHandler<byte[]>() {
- public byte[] handleResponse(HttpResponse response)
- throws ClientProtocolException, IOException {
- HttpEntity entity = response.getEntity();
- if (entity != null) {
- return EntityUtils.toByteArray(entity);
- } else {
- return null;
- }
- }
- };
-
- byte[] charts = httpClient.execute(get, handler);
- FileOutputStream out = new FileOutputStream(fileName);
- out.write(charts);
- out.close();
-
- httpClient.getConnectionManager().shutdown();