Android 台灣中文網

標題: android http下載的簡單例子 [打印本頁]

作者: 暗桌之光    時間: 2011-6-12 13:24
標題: android http下載的簡單例子
  1. package org.apache.http.examples.client;

  2. import org.apache.http.Header;
  3. import org.apache.http.HttpEntity;
  4. import org.apache.http.HttpHost;
  5. import org.apache.http.HttpResponse;
  6. import org.apache.http.client.HttpClient;
  7. import org.apache.http.client.methods.HttpGet;
  8. import org.apache.http.conn.params.ConnRoutePNames;
  9. import org.apache.http.impl.client.DefaultHttpClient;
  10. import org.apache.http.util.EntityUtils;

  11. public class ClientExecuteProxy {

  12.     public static void main(String[] args)throws Exception {

  13.         HttpHost proxy = new HttpHost( "10.0.0.172", 80, "http");
  14.         HttpHost target = new HttpHost("YOUR_TARGET_IP", 80, "http");        

  15.         DefaultHttpClient httpclient = new DefaultHttpClient();
  16.         httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

  17.         
  18.         HttpGet req = new HttpGet("/");

  19.         System.out.println("executing request to " + target + " via " + proxy);
  20.         HttpResponse rsp = httpclient.execute(target, req);
  21.         HttpEntity entity = rsp.getEntity();

  22.         System.out.println("----------------------------------------");
  23.         System.out.println(rsp.getStatusLine());
  24.         Header[] headers = rsp.getAllHeaders();
  25.         for (int i = 0; i<headers.length; i++) {
  26.             System.out.println(headers[i]);
  27.         }
  28.         System.out.println("----------------------------------------");

  29.         if (entity != null) {
  30.             System.out.println(EntityUtils.toString(entity));
  31.         }

  32.         // When HttpClient instance is no longer needed,
  33.         // shut down the connection manager to ensure
  34.         // immediate deallocation of all system resources
  35.         httpclient.getConnectionManager().shutdown();        
  36.     }

  37. }
複製代碼







歡迎光臨 Android 台灣中文網 (https://apk.tw/) Powered by Discuz! X3.1