当前位置:首页 > 开发 > 开源软件 > 正文

HttpClient4.3.5使用代理创建实例并发送请求

发表于: 2015-01-13   作者:coffee_yan   来源:转载   浏览次数:
摘要: public String xmlHttpPost(String requestInfo, String urlAddress, boolean isProxy, String proxyHost, int proxyPort, ContentType contentType) { String responseInfo = null; InputStream inpu
	public String xmlHttpPost(String requestInfo, String urlAddress, boolean isProxy, 
			String proxyHost, int proxyPort, ContentType contentType) {
		String responseInfo = null;
		InputStream inputResStream = null;
		try {
			CloseableHttpClient httpClient = null;
			if (isProxy) {
				HttpHost proxy = new HttpHost(proxyHost, proxyPort);
				DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
				httpClient = HttpClients.custom().setRoutePlanner(routePlanner).build();
			}else{
				httpClient = HttpClients.createDefault();
			}
			StringEntity stringEntity = new StringEntity(requestInfo, contentType);
			HttpPost post = new HttpPost(urlAddress);
			post.setEntity(stringEntity);

			log.info("urlAddress: " + urlAddress.trim() + " proxyHost: " + proxyHost.trim() + " proxyPort: " + proxyPort);
			CloseableHttpResponse response = httpClient.execute(post);
			try {
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					inputResStream = entity.getContent();
					try {
						BufferedReader br = new BufferedReader(new InputStreamReader(inputResStream));
						StringBuffer resBuffer = new StringBuffer();
						String resTemp = "";
						while ((resTemp = br.readLine()) != null) {
							resBuffer.append(resTemp);
						}
						responseInfo = resBuffer.toString();
					} finally {
						inputResStream.close();
					}
				}
			} finally {
				response.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return responseInfo;
	}

HttpClient4.3.5使用代理创建实例并发送请求

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
使用Fiddler发送POST请求 在测试过程中,有时会遇到需要修改请求中带的参数,去验证权限的安全问题
佟强 http://blog.csdn.net/microtong OakCMS内容管理系统 http://www.oakcms.cn http://www.oakcms
1、客户端代码 <html> <head> <script> var callbackFunction = function(data){
java使用jpcap抓包工具获得本机发送的所有tcp请求,根据tcp请求的内容可以调试某个应用启动后对外发
java使用jpcap抓包工具获得本机发送的所有tcp请求,根据tcp请求的内容可以调试某个应用启动后对外发
test.vbs文件代码如下图: 1 ScriptTimeout = 120 2 function getHTTPPage(url) 3 on error resume
在android应用程序中,可以使用HttpURLConnection发送HTTP请求。详见如下实例 1、activity_main.xml
package com.liuyu.test; import java.io.BufferedReader; import java.io.IOException; import jav
这两天使用httpclient4.3.5(好像httpclient每个版本差别挺大的)访问一个https的服务,发现跑一段时
精灵类是Sprite,它的类图如下图所示: Sprite类直接继承了Node类,具有Node基本特征。此外,我们还
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号