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

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

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号