求android http下载的简单例子

RT

  • jy02305022 - 1年前

    package org.apache.http.examples.client;
    
    import org.apache.http.Header;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpHost;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.conn.params.ConnRoutePNames;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;
    
    public class ClientExecuteProxy {
    
        public static void main(String[] args)throws Exception {
    
            HttpHost proxy = new HttpHost( "10.0.0.172", 80, "http");
            HttpHost target = new HttpHost("YOUR_TARGET_IP", 80, "http");        
    
            DefaultHttpClient httpclient = new DefaultHttpClient();
            httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    
            
            HttpGet req = new HttpGet("/");
    
            System.out.println("executing request to " + target + " via " + proxy);
            HttpResponse rsp = httpclient.execute(target, req);
            HttpEntity entity = rsp.getEntity();
    
            System.out.println("----------------------------------------");
            System.out.println(rsp.getStatusLine());
            Header[] headers = rsp.getAllHeaders();
            for (int i = 0; i<headers.length; i++) {
                System.out.println(headers[i]);
            }
            System.out.println("----------------------------------------");
    
            if (entity != null) {
                System.out.println(EntityUtils.toString(entity));
            }
    
            // When HttpClient instance is no longer needed, 
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();        
        }
    
    }