採用HTTPClient通過代理串連伺服器

來源:互聯網
上載者:User

http://www.knowsky.com/366052.html

 

使用代理一般的辦法是用HTTPConnection的靜態方法setProxyServer實現:
  
  HTTPConnection.setProxyServer("my.proxy.dom", 8008);
  
  調用該方法以後產生的HTTPConnection對象都會通過該代理建立伺服器串連。
  
  特定某一個串連使用代理:
  setCurrentProxy()
  
  你也可以設定串連某些伺服器不要採用代理:
  
  HTTPConnection.dontProxyFor("localhost");
  
  假如Proxy 伺服器要求使用者名稱密碼認證:
  
  AuthorizationInfo.addDigestAuthorization(host, proxyPort, "", name, pass);
  
  其中第三個參數是認證域,一般Proxy 伺服器可以設為空白字串,除非你知道伺服器的確切域。
  
  還有另外一個方法就是使用DefaultAuthHandler:
  
  DefaultAuthHandler.setAuthorizationPrompter(new MyAuthPrompter(pa_name, pa_pass));
  
  MyAuthPrompter是實現了AuthorizationPrompter介面的自訂類:
  
  class MyAuthPrompter implements AuthorizationPrompter
  
  {
  
  private String pa_name, pa_pass;
  
  private boolean been_here = false;
  
  MyAuthPrompter(String pa_name, String pa_pass) {
  
  this.pa_name = pa_name;
  
  this.pa_pass = pa_pass;
  
  }
  
  public NVPair getUsernamePassWord(AuthorizationInfo challenge, boolean forProxy) {
  
  if (forProxy && pa_name != null){
  
  if (been_here) {
  
  System.out.println("Proxy authorization failed");
  
  return null;
  
  }
  
  been_here = true;
  
  return new NVPair(pa_name, pa_pass);
  
  }
  
  if (been_here)  {
  
  System.out.println("Proxy authorization sUCceeded");
  
  }
  
  // print out all challenge info
  
  if (forProxy)
  
  System.out.println("The proxy requires authorization");
  
  else
  
  System.out.println("The server requires authorization for this resource");
  
  return null;
  
  }
  
  }
  
  關於頁面認證
  一個頁面是否需要認證,以及要求認證的資訊可以通過HTTPClient/doc/GetAuthInfo.Java來擷取:
  
  java GetAuthInfo http://some.host.dom/the/file.Html
  
  程式會輸出認證資訊包括認證域。

 

 

HttpClient入門

http://www.ibm.com/developerworks/cn/opensource/os-httpclient/#rate

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.