部落客廣告聯播

2007年7月12日 星期四

筆記:Jakarta HttpClient library (Notes: Jakarta HttpClient library)

  1. 需要四個Jar檔:
    • commons-httpclient.jar
    • commons-lang.jar
    • commons-logging.jar
    • commons-codec.jar
  2. 簡單範例:

    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.methods.GetMethod;

    public class test{
    public static void main(String[] args)
    {
    try{
    HttpClient client=new HttpClient();
    GetMethod method=new GetMethod("http://www.hinet.net");
    int resultCode=client.executeMethod(method);
    if(resultCode==200)
    System.out.println(method.getResponseAsString());
    }catch(Exception e)
    {}
    }
    }
  3. HttpClient類別依賴
    • HttpConnectionManager介面
      • SimpleHttpConnectionManager
      • MultithreadedConnectionManager:需要同時做多個連結,預設連線pool是每個host有兩個,可調整最大數為20
    • HttpClientParams類別
  4. HttpMethod-->HttpMethodBase --> GetMethod、PostMethod等。(位於org.apache.commons.httpclient.methods)
  5. org.apache.commons.httpclient.auth.AuthScheme的實作有:
    • BasicScheme
    • DigestScheme
    • NTLMScheme
  6. AuthState:
    • 提供authentication處理狀態
    • 更換authentication scheme
  7. AuthPolicy
  8. CredentialsProvider:
    • 用來向使用者要求憑證
  9. AuthChallengeParser、AuthChallengeProcessor:
    • 在authentication處理中解析response header
    • 處理在header中的盤問(challenge)
  10. authendication處理相關Exception:
    • AuthChallengeException
    • MalformedChallengeException
    • InvalidCredentialsException
    • CredentialsNotAvaliableException
  11. org.apache.commons.httpclient.cookie package裡包含用來處理在HttpClient物件裡heder的cookie。
  12. org.apache.commons.httpclient.params裡包含有許多HttpClient重要物件的參數,比如說HttpClientParams就是用來設定HttpClient類別物件的類別。
  13. 以下程式碼,可將送出的request中header的browser資訊修改掉:

    HttpClient client=new HttpClient();
    client.getParams().setParameter("http.useragent", "java browser");

  14. 用GetMethod取回GIF圖檔:


  15. import java.io.FileOutputStream;

    import org.apache.commons.httpclient.HostConfiguration;
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.URI;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.apache.commons.httpclient.protocol.Protocol;


    public class test {


    public static void main(String[] args) {
    try {
    HttpClient client=new HttpClient();
    client.getParams().setParameter("http.useragent", "java browser");
    GetMethod method=new GetMethod();
    method.setURI(new URI("http://tw.yimg.com/i/tw/dictionary/masthead_logo.gif"));
    HostConfiguration hostConfig=new HostConfiguration();
    hostConfig.setHost("tw.yimg.com",null,80,Protocol.getProtocol("http"));
    int result=client.executeMethod(hostConfig,method);
    System.out.printf("result code=%d",result);
    byte[] gif=method.getResponseBody();
    FileOutputStream fos=new FileOutputStream("c:\\image.gif");
    fos.write(gif);
    fos.close();
    } catch (Exception e) {
    // TODO: handle exception
    }


    }

    }

    在這個範例程式,如果要抓下來的圖檔很大,可以用method.getResponseBodyAsStream()取得InputStream後直接寫入檔案,就不用放在記憶體中了。
  16. 上面都是用GetMethod執行,可改用PostMethod,而原本給GetMethod的建構子或setUrl方法的字串中的參數,可用PostMethod物件的addParameter( )即可。
  17. 若要以HttpClient做上傳資料動作,可改用MultiPartMethod物件。
  18. PutMethod主要用來把檔案放上server,DeleteMethod則相反。
  19. HttpClient預設使用SimpleHttpConnectionManager,若需同時發出多個作業,要改用MultiThreadedHttpConnectionManager。
  20. multiThread範例:

    import java.io.BufferedOutputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;

    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
    import org.apache.commons.httpclient.params.HttpConnectionParams;


    public class MultiThread extends Thread {

    private HttpClient client;
    private GetMethod method;

    public MultiThread(HttpClient client,GetMethod method)
    {
    this.client=client;
    this.method=method;
    }

    public static void main(String[] args) {
    MultiThreadedHttpConnectionManager mcm=new MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams params=new HttpConnectionManagerParams();
    params.setDefaultMaxConnectionsPerHost(10);
    mcm.setParams(params);

    HttpClient client=new HttpClient(mcm);
    GetMethod method1=new GetMethod("http://www.sun.com/images/l2/l2_phone_pda.gif");
    GetMethod method2=new GetMethod("http://www.sun.com/images/l2/l2_swingap.gif");
    GetMethod method3=new GetMethod("http://java.sun.com/javase/images/dukejdk6_100x88.gif");

    new MultiThread(client,method1).start();
    new MultiThread(client,method2).start();
    new MultiThread(client,method3).start();
    }

    @Override
    public void run() {
    try{
    client.executeMethod(method);

    String filePath="c:\\"+method.getURI().getName();
    InputStream input=method.getResponseBodyAsStream();
    BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(filePath));
    int data;
    while((data=input.read())>=0)
    bos.write(data);

    input.close();
    bos.close();

    }catch(Exception e)
    {
    System.out.println(e);
    }
    }

    }

  21. HttpClient支援NTLM、Digest、Basic三種authentication。
  22. authentication範例1

    import org.apache.commons.httpclient.*;
    import org.apache.commons.httpclient.auth.*;
    import org.apache.commons.httpclient.method.*;

    public class authExam{
    public static void main(String[] args) throws Exception{
    HttpClient client=new HttpClient();
    HostConfiguration host=client.getHostConfiguration();
    host.setHost(new URI("http://localhost:8080",true));

    GetMethod method=new GetMethod("protectedArea/protect.jsp");
    try{
    int code=client.executeMethod(host,method);
    if (code==HttpStatus.SC_UNAUTHORIZED)
    {
    Crenditals cred=new UsernamePasswordCrenditals("tomcat","tomcat");
    AuthScope scope=new AuthScope(host.getHost(),host.getPort());
    client.getState().setCrentials(scope,cred);
    client.executeMethod(host,method);
    }
    }catch(Exception e){}
    }
    }
  23. authentication 範例2

    public class authExam2{
    public static void main(String args[]) throws Exception
    {
    HttpClient client=new HttpClient();
    HostConfiguration host=client.getHostConfiguration();
    host.setHost(new URI("http://localhost:8080",true));

    Credentials cre=new UsernamePasswordCredential("tomcat","tomcat");
    AuthoScope scope=new AuthScope(host.getHost(),host.getPort());
    client.getState().setCredentials(scope,cre);

    try{
    client.executeMethod(new GetMethod("http://localhost/protectedArea");)
    }
    catch(Exception e){}
    }
    }
  24. 讀取Cookie
    client.getState( ).getCookies( );

沒有留言: