HttpClient Examples:Request via a proxy

官方主頁:http://hc.apache.org/

 

Components

Request via a proxy

This example demonstrates how to send an HTTP request via a proxy.

 

请求通过一个代理

 

这个例子示范怎样去发送一个HTTP请求通过一个代理

 

package cn.lake.util;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.scheme.SocketFactory;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;

/**
 * How to send a request via proxy using {@link HttpClient HttpClient}.
 *
 * @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
 *
 *
 * <!-- empty lines above to avoid 'svn diff' context problems -->
 * @version $Revision$
 *
 * @since 4.0
 */
public class ClientExecuteProxy {

	/**
	 * The default parameters.
	 * Instantiated in {@link #setup setup}.
	 */
	private static HttpParams defaultParameters = null;

	/**
	 * The scheme registry.
	 * Instantiated in {@link #setup setup}.
	 */
	private static SchemeRegistry supportedSchemes;

	/**
	 * Main entry point to this example.
	 *
	 * @param args      ignored
	 */
	public final static void main(String[] args) throws Exception {

		// make sure to use a proxy that supports CONNECT
		final HttpHost target = new HttpHost("issues.apache.org", 443, "https");
		final HttpHost proxy = new HttpHost("127.0.0.1", 8666, "http");

		setup(); // some general setup

		HttpClient client = createHttpClient();

		client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

		HttpRequest req = createRequest();

		System.out.println("executing request to " + target + " via " + proxy);
		HttpEntity entity = null;
		try {
			HttpResponse rsp = client.execute(target, req);
			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 (rsp.getEntity() != null) {
				System.out.println(EntityUtils.toString(rsp.getEntity()));
			}

		} finally {
			// If we could be sure that the stream of the entity has been
			// closed, we wouldn't need this code to release the connection.
			// However, EntityUtils.toString(...) can throw an exception.

			// if there is no entity, the connection is already released
			if (entity != null)
				entity.consumeContent(); // release connection gracefully
		}
	} // main

	private final static HttpClient createHttpClient() {

		ClientConnectionManager ccm = new ThreadSafeClientConnManager(getParams(), supportedSchemes);
		//  new SingleClientConnManager(getParams(), supportedSchemes);

		DefaultHttpClient dhc = new DefaultHttpClient(ccm, getParams());

		return dhc;
	}

	/**
	 * Performs general setup.
	 * This should be called only once.
	 */
	private final static void setup() {

		supportedSchemes = new SchemeRegistry();

		// Register the "http" and "https" protocol schemes, they are
		// required by the default operator to look up socket factories.
		SocketFactory sf = PlainSocketFactory.getSocketFactory();
		supportedSchemes.register(new Scheme("http", sf, 80));
		sf = SSLSocketFactory.getSocketFactory();
		supportedSchemes.register(new Scheme("https", sf, 80));

		// prepare parameters
		HttpParams params = new BasicHttpParams();
		HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
		HttpProtocolParams.setContentCharset(params, "UTF-8");
		HttpProtocolParams.setUseExpectContinue(params, true);
		defaultParameters = params;

	} // setup

	private final static HttpParams getParams() {
		return defaultParameters;
	}

	/**
	 * Creates a request to execute in this example.
	 *
	 * @return  a request without an entity
	 */
	private final static HttpRequest createRequest() {

		HttpRequest req = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
		//("OPTIONS", "*", HttpVersion.HTTP_1_1);

		return req;
	}

}

 

 

翻譯不好,請見諒!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值