httpclient 인 스 턴 스

79517 단어 자바httpclienthtml
새 HttpClient 4.2 는 이전 3.x 버 전과 큰 변화 가 있 었 으 므 로http://hc.apache.org/에서 최신 정 보 를 얻 는 것 을 권장 합 니 다.
HttpCore 와 HttpClient:HttpCore 는 HTTP 전송 구성 요소 에 있 는 바 텀 패키지 로 HTTP 클 라 이언 트 와 서버 의 개발 을 간소화 할 수 있 습 니 다.HttpClient 는 HTTP 1.1 버 전 으로 HttpCore 패키지 기반 의 구현 입 니 다.이 는 클 라 이언 트 인증,HTTP 상태 관리,HTTP 연결 관리 에 재 활용 가능 한 클 라 이언 트 구성 요 소 를 제공 합 니 다.HttpCore 패키지 의 현재 최신 버 전 은 httpcore-4.2.4 입 니 다.HttpClient 패키지 의 버 전 은 httpclient-4.2.5 입 니 다.HttpCore 패키지 와 HttpClient 패키지 의 차 이 를 알 게 되면 프로그램 에서 어떤 종류의 라 이브 러 리 에 존재 하 는 지 대체적으로 알 아야 합 니 다.예 를 들 어 org.apache.http 패 키 지 는 HttpCore 에 속 하고 org.apache.http.client 패 키 지 는 HttpClient 에 속 합 니 다.
HttpClient 의 API 문 서 는 다운로드 한 zip 에 포함 되 어 있 습 니 다.HttpCore 의 API 문 서 는 다음 을 참고 할 수 있 습 니 다.http://hc.apache.org/httpcomponents-core-4.2.x/httpcore/apidocs/index.htmlHttpClient 4.2 는 자바 5.0 이상 버 전이 필요 합 니 다.지원 패키지 가 필요 합 니 다(zip 패키지 에 포함 되 어 있 음 다운로드):*Apache HttpComponents HttpCore*Apache Commons Logging*Apache Commons Codec1.HTML 페이지 의 내용,간단 한 get 응용 프로그램 가 져 오기
    //     HTML     ,     get  
    public void grabPageHTML() throws Exception {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://www.baidu.com/");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        String html = EntityUtils.toString(entity, "GBK");
        
        // releaseConnection   reset,     request   ,         。
        //        HttpGet             ;         。
        httpget.releaseConnection();

        System.out.println(html);
    }


2.로 컬 에 파일 다운로드(이 시범 에 서 는 인증 코드 그림)
    //          (            )
    public void downloadFile() throws Exception {
        String url = "http://www.lashou.com/account/captcha";
        String destfilename = "D:\\TDDOWNLOAD\\yz.png";
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);
        File file = new File(destfilename);
        if (file.exists()) {
            file.delete();
        }
        
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        InputStream in = entity.getContent();
        try {
            FileOutputStream fout = new FileOutputStream(file);
            int l = -1;
            byte[] tmp = new byte[2048]; 
            while ((l = in.read(tmp)) != -1) {
                fout.write(tmp);
            } 
            fout.close();
        } finally {
            //   InputStream  HttpEntity ,        。
            in.close();
        }
        
        httpget.releaseConnection();
    }


3.Post 방법,아 날로 그 폼 제출 매개 변 수 를 사이트 에 로그 인하 고 회원 페이지 를 열 어 내용 가 져 오기(세 션 유지)
    // Post  ,             。
    //          :grabPageHTML/downloadFile,     Post   。
    public void login2Lashou() throws Exception {
        //
        String url = "http://www.lashou.com/account/captcha";
        String destfilename = "D:\\TDDOWNLOAD\\yz.png";
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);
        File file = new File(destfilename);
        if (file.exists()) {
            file.delete();
        }
        
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        InputStream in = entity.getContent();
        try {
            FileOutputStream fout = new FileOutputStream(file);
            int l = -1;
            byte[] tmp = new byte[2048]; 
            while ((l = in.read(tmp)) != -1) {
                fout.write(tmp);
            } 
            fout.close();
        } finally {
            in.close();
        }
        httpget.releaseConnection();

// : Post , 、 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println(" ..."); String yan = br.readLine(); HttpPost httppost = new HttpPost("http://www.lashou.com/account/login/"); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("user_id", "testuser007")); params.add(new BasicNameValuePair("pwd", "asdfg123")); params.add(new BasicNameValuePair("yan", yan)); params.add(new BasicNameValuePair("save_user", "on")); params.add(new BasicNameValuePair("save_pwd", "on")); params.add(new BasicNameValuePair("sub", " ")); httppost.setEntity(new UrlEncodedFormEntity(params)); response = httpclient.execute(httppost); entity = response.getEntity(); // Jsoup , String postResult = EntityUtils.toString(entity, "GBK"); // Cookie 。 List<Cookie> cookies = ((AbstractHttpClient)httpclient).getCookieStore().getCookies(); for(Cookie cookie: cookies) System.out.println(cookie); httppost.releaseConnection();
// : ( ) String memberpage = "http://www.lashou.com/account/orders/"; httpget = new HttpGet(memberpage); response = httpclient.execute(httpget); // HttpClient! entity = response.getEntity(); String html = EntityUtils.toString(entity, "GBK"); httpget.releaseConnection(); System.out.println(html); }


출력:
다운로드 한 인증 코드 에 표 시 된 숫자 를 입력 하 십시오...sbzq...
[version: 0][name: login_name2][value: testuser007][domain:  www.lashou.com][path : /][expiry: Mon Sep 09 10:21:19 CST 2013][version: 0][name: pwd2][value: 4c88a4062736c26572d3ec382868fa2b][domain: lashou.com][path: /][expiry: Mon Sep 09 10:21:19 CST 2013]?
...
4.프 록 시 설정
    //        
    public void testProxy() throws Exception {
        HttpHost proxy = new HttpHost("127.0.0.1", 8888);
        
        //    
        HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        
        //    
        HttpParams params = new BasicHttpParams(); 
        params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        HttpClient httpclient1 = new DefaultHttpClient(params);
    }


5.자주 사용 하 는 HTTP 헤드 설정
    //     HTTP    
    public void testBasicHeader() throws Exception {
        HttpParams params = new BasicHttpParams(); 
        Collection<BasicHeader> collection = new ArrayList<BasicHeader>();
        collection.add(new BasicHeader("Accept", "text/html, application/xhtml+xml, */*"));
        collection.add(new BasicHeader("Referer", "http://www.sina.com/"));
        collection.add(new BasicHeader("Accept-Language", "zh-CN"));
        collection.add(new BasicHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"));
        collection.add(new BasicHeader("Accept-Encoding", "gzip, deflate"));
        params.setParameter(ClientPNames.DEFAULT_HEADERS, collection);
        
        HttpClient httpclient = new DefaultHttpClient(params);
        
        //      
    }


6.다 중 스 레 드 프로 그래 밍 의 스 레 드 풀 설정
    //             (           HttpClient                )
    public void testConnectionManager() throws Exception {
        //      
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
        schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));

        PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
        cm.setMaxTotal(200); //           
        cm.setDefaultMaxPerRoute(20); //             
        HttpHost localhost = new HttpHost("locahost", 80); //                 
        cm.setMaxPerRoute(new HttpRoute(localhost), 30);

        //     
        HttpParams params = new BasicHttpParams(); 
        params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
        HttpClient httpclient = new DefaultHttpClient(cm, params);
        
        //      
    }


7.HTTP 컨 텍스트 대상 테스트(HttpContext)
    //   HTTP     (HttpContext)
    public void testContext() throws Exception {
        //
        DefaultHttpClient httpclient = new DefaultHttpClient();

        HttpContext localContext = new BasicHttpContext();
        HttpGet httpget = new HttpGet("http://www.baidu.com/");

        HttpResponse response = httpclient.execute(httpget, localContext);
        
        // the actual connection to the target server.
        HttpConnection conn = (HttpConnection) localContext.getAttribute(
            ExecutionContext.HTTP_CONNECTION);  
        System.out.println("Socket timeout: " + conn.getSocketTimeout());  

        // the connection target
        HttpHost target = (HttpHost) localContext.getAttribute(
            ExecutionContext.HTTP_TARGET_HOST);
        System.out.println("Final target: " + target);
        
        // the connection proxy, if used 
        HttpHost proxy = (HttpHost) localContext
                .getAttribute(ExecutionContext.HTTP_PROXY_HOST);
        if (proxy != null)
            System.out.println("Proxy host/port: " + proxy.getHostName() + "/"
                    + proxy.getPort());

        // the actual HTTP request
        HttpRequest request = (HttpRequest) localContext
                .getAttribute(ExecutionContext.HTTP_REQUEST);
        System.out.println("HTTP version: " + request.getProtocolVersion());
        Header[] headers = request.getAllHeaders();
        System.out.println("HTTP Headers: ");
        for (Header header : headers) {
            System.out.println("\t" + header.getName() + ": " + header.getValue());
        }
        System.out.println("HTTP URI: " + request.getRequestLine().getUri());

        // the actual HTTP response
        response = (HttpResponse) localContext
                .getAttribute(ExecutionContext.HTTP_RESPONSE);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            System.out.println("Content Encoding:" + entity.getContentEncoding());
            System.out.println("Content Type:" + entity.getContentType());
        }
        
        // the flag indicating whether the actual request has been fully transmitted to the connection target. 
        System.out.println("Sent flag: " + localContext.getAttribute(ExecutionContext.HTTP_REQ_SENT));

// entity , , 。 entity = response.getEntity(); EntityUtils.consume(entity); }


출력:
Socket timeout: 0Final target:  http://www.baidu.com HTTP version: HTTP/1.1HTTP Headers:     Host:  www.baidu.com     Connection: Keep-Alive    User-Agent: Apache-HttpClient/4.2.5 (java 1.5)HTTP URI: /Content Encoding:nullContent Type:Content-Type: text/html;charset=utf-8Sent flag: true
8.완전한 코드

package com.clzhang.sample.net;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpConnection;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
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.conn.params.ConnRoutePNames;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

public class HttpClientSample1 {
    
    //     HTML     ,     get  
    public void grabPageHTML() throws Exception {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://www.baidu.com/");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        String html = EntityUtils.toString(entity, "GBK");
        
        // releaseConnection   reset,     request   ,         。
        //        HttpGet             ;         。
        httpget.releaseConnection();

        System.out.println(html);
    }
    
    //          (            )
    public void downloadFile() throws Exception {
        String url = "http://www.lashou.com/account/captcha";
        String destfilename = "D:\\TDDOWNLOAD\\yz.png";
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);
        File file = new File(destfilename);
        if (file.exists()) {
            file.delete();
        }
        
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        InputStream in = entity.getContent();
        try {
            FileOutputStream fout = new FileOutputStream(file);
            int l = -1;
            byte[] tmp = new byte[2048]; 
            while ((l = in.read(tmp)) != -1) {
                fout.write(tmp);
            } 
            fout.close();
        } finally {
            //   InputStream  HttpEntity ,        。
            in.close();
        }
        
        httpget.releaseConnection();
    }
    
    // Post  ,             。
    //          :grabPageHTML/downloadFile,     Post   。
    public void login2Lashou() throws Exception {
        //
        String url = "http://www.lashou.com/account/captcha";
        String destfilename = "D:\\TDDOWNLOAD\\yz.png";
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);
        File file = new File(destfilename);
        if (file.exists()) {
            file.delete();
        }
        
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        InputStream in = entity.getContent();
        try {
            FileOutputStream fout = new FileOutputStream(file);
            int l = -1;
            byte[] tmp = new byte[2048]; 
            while ((l = in.read(tmp)) != -1) {
                fout.write(tmp);
            } 
            fout.close();
        } finally {
            in.close();
        }
        httpget.releaseConnection();
        
        //    : Post           ,                 、  
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("                 ...");
        String yan = br.readLine();

        HttpPost httppost = new HttpPost("http://www.lashou.com/account/login/");
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("user_id", "testuser007"));
        params.add(new BasicNameValuePair("pwd", "asdfg123"));
        params.add(new BasicNameValuePair("yan", yan));
        params.add(new BasicNameValuePair("save_user", "on"));
        params.add(new BasicNameValuePair("save_pwd", "on"));
        params.add(new BasicNameValuePair("sub", "  "));
        httppost.setEntity(new UrlEncodedFormEntity(params));
        
        response = httpclient.execute(httppost);
        entity = response.getEntity();
        //       Jsoup              ,         
        String postResult = EntityUtils.toString(entity, "GBK"); 
        //               Cookie          。
        List<Cookie> cookies = ((AbstractHttpClient)httpclient).getCookieStore().getCookies();
        for(Cookie cookie: cookies)
            System.out.println(cookie);
        httppost.releaseConnection();
        
        //    :             (              )
        String memberpage = "http://www.lashou.com/account/orders/";
        httpget = new HttpGet(memberpage);
        response = httpclient.execute(httpget); //       HttpClient!
        entity = response.getEntity();
        String html = EntityUtils.toString(entity, "GBK");
        httpget.releaseConnection();

        System.out.println(html);
    }
    
    //        
    public void testProxy() throws Exception {
        HttpHost proxy = new HttpHost("127.0.0.1", 8888);
        
        //    
        HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        
        //    
        HttpParams params = new BasicHttpParams(); 
        params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        HttpClient httpclient1 = new DefaultHttpClient(params);
    }
    
    //     HTTP    
    public void testBasicHeader() throws Exception {
        HttpParams params = new BasicHttpParams(); 
        Collection<BasicHeader> collection = new ArrayList<BasicHeader>();
        collection.add(new BasicHeader("Accept", "text/html, application/xhtml+xml, */*"));
        collection.add(new BasicHeader("Referer", "http://www.sina.com/"));
        collection.add(new BasicHeader("Accept-Language", "zh-CN"));
        collection.add(new BasicHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"));
        collection.add(new BasicHeader("Accept-Encoding", "gzip, deflate"));
        params.setParameter(ClientPNames.DEFAULT_HEADERS, collection);
        
        HttpClient httpclient = new DefaultHttpClient(params);
        
        //      
    }
    
    //             (           HttpClient                )
    public void testConnectionManager() throws Exception {
        //      
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
        schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));

        PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
        cm.setMaxTotal(200); //           
        cm.setDefaultMaxPerRoute(20); //             
        HttpHost localhost = new HttpHost("locahost", 80); //                 
        cm.setMaxPerRoute(new HttpRoute(localhost), 30);

        //     
        HttpParams params = new BasicHttpParams(); 
        params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
        HttpClient httpclient = new DefaultHttpClient(cm, params);
        
        //      
    }
    
    //   HTTP     (HttpContext)
    public void testContext() throws Exception {
        //
        DefaultHttpClient httpclient = new DefaultHttpClient();

        HttpContext localContext = new BasicHttpContext();
        HttpGet httpget = new HttpGet("http://www.baidu.com/");

        HttpResponse response = httpclient.execute(httpget, localContext);
        
        // the actual connection to the target server.
        HttpConnection conn = (HttpConnection) localContext.getAttribute(
            ExecutionContext.HTTP_CONNECTION);  
        System.out.println("Socket timeout: " + conn.getSocketTimeout());  

        // the connection target
        HttpHost target = (HttpHost) localContext.getAttribute(
            ExecutionContext.HTTP_TARGET_HOST);
        System.out.println("Final target: " + target);
        
        // the connection proxy, if used 
        HttpHost proxy = (HttpHost) localContext
                .getAttribute(ExecutionContext.HTTP_PROXY_HOST);
        if (proxy != null)
            System.out.println("Proxy host/port: " + proxy.getHostName() + "/"
                    + proxy.getPort());

        // the actual HTTP request
        HttpRequest request = (HttpRequest) localContext
                .getAttribute(ExecutionContext.HTTP_REQUEST);
        System.out.println("HTTP version: " + request.getProtocolVersion());
        Header[] headers = request.getAllHeaders();
        System.out.println("HTTP Headers: ");
        for (Header header : headers) {
            System.out.println("\t" + header.getName() + ": " + header.getValue());
        }
        System.out.println("HTTP URI: " + request.getRequestLine().getUri());

        // the actual HTTP response
        response = (HttpResponse) localContext
                .getAttribute(ExecutionContext.HTTP_RESPONSE);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            System.out.println("Content Encoding:" + entity.getContentEncoding());
            System.out.println("Content Type:" + entity.getContentType());
        }
        
        // the flag indicating whether the actual request has been fully transmitted to the connection target. 
        System.out.println("Sent flag: " + localContext.getAttribute(ExecutionContext.HTTP_REQ_SENT));
        
        //         entity    ,        ,            。
        entity = response.getEntity();
        EntityUtils.consume(entity);
    }    
    
    public static void main(String[] args) throws Exception {
        HttpClientSample1 ins = new HttpClientSample1();
        
//        ins.grabPageHTML();
//        ins.downloadFile();
        ins.login2Lashou();
//        ins.testContext();
    }
}


 
 
다음으로 이동:http://www.cnblogs.com/nayitian/archive/2013/08/26/3282029.html

좋은 웹페이지 즐겨찾기