`

httpclient 代理访问的小例子

阅读更多

 

 

 

package com;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.xml.ws.Response;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;

public class pass {
	public static void main(String args[]) throws IOException, IOException{
		
		DefaultHttpClient httpclient= new DefaultHttpClient();
		HttpHost targetHost= new HttpHost("www.baidu.com");
		HttpHost proxy = new HttpHost("200.132.0.70",3124);  // 这两句话去掉就是直接访问不用代理
		httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); // 这两句话去掉就是直接访问不用代理
		HttpGet httpget= new HttpGet("/");
		System.out.println("目标:"+targetHost);
		System.out.println("请求"+httpget.getRequestLine());
		HttpResponse response = httpclient.execute(targetHost,httpget);
		HttpEntity entity =response.getEntity();
		System.out.println("------------------");
		System.out.println(response.getStatusLine());
		if(entity !=null){
			System.out.println("response content length" +entity.getContentLength());
			
		}
		BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(),"UTF-8"));
		String line = null;
		while ((line = reader.readLine())!=null){
			System.out.println(line);
			
		}
		if(entity !=null){
			entity.consumeContent();
		}
		
		}
		
		
	}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics