`
JadeLuo
  • 浏览: 425643 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
社区版块
存档分类
最新评论

java http proxy request demo

    博客分类:
  • java
 
阅读更多
package com.ygsoft.community.regulation.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class HttpUtil {

	public static void main(String args[]) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("flag", "0");
		map.put("TypeID", "root_gr_ztfl_hj");
		map.put("pageNo", "1");
		map.put("pageSize", "100");
		new HttpUtil().post(map, "http://wsbs.jinwan.gov.cn/wsbs/f/matterList");
	}

	private String proxyHost;

	private int proxyPort;

	public HttpUtil() {
		this.proxyHost = "10.1.5.78";
		this.proxyPort = 3128;
	}

	public HttpUtil(String proxyHost, Integer proxyPort) {
		this.proxyHost = proxyHost;
		this.proxyPort = proxyPort;
	}

	@SuppressWarnings("rawtypes")
	public String post(Map buzReqParams, String reqUrl) throws Exception {
		String result = null;
		// 创建HttpClientBuilder
		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
		// HttpClient
		CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
		// 依次是目标请求地址,端口号,协议类型
		HttpHost target = new HttpHost(reqUrl, 80, "http");
		// 依次是代理地址,代理端口号,协议类型
		HttpHost proxy = new HttpHost(this.proxyHost, this.proxyPort, "http");
		RequestConfig config = RequestConfig.custom().setProxy(proxy).build();

		// 请求地址
		HttpPost httpPost = new HttpPost(reqUrl);
		httpPost.setConfig(config);
		// 创建参数队列
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		Set set = buzReqParams.keySet();
		Iterator it = set.iterator();
		while (it.hasNext()) {
			String key = (String) it.next();
			formparams.add(new BasicNameValuePair(key, (String) buzReqParams
					.get(key)));
		}
		UrlEncodedFormEntity entity;
		try {
			entity = new UrlEncodedFormEntity(formparams, "UTF-8");
			httpPost.setEntity(entity);
			CloseableHttpResponse response = closeableHttpClient.execute(
					target, httpPost);
			HttpEntity httpEntity = response.getEntity();
			if (httpEntity != null) {
				result = EntityUtils.toString(httpEntity, "UTF-8");
				// 打印响应内容
				System.out.println("response:" + result);
			}
			// 释放资源
			closeableHttpClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}

 

分享到:
评论

相关推荐

    icedemo实现对象的传输与返回(java)

    在"icedemo"项目中,我们首先要创建Request和Response类。这些类可以是普通的Java类,通过序列化和反序列化机制与ICE通信。例如: ```java public class Request { private String methodName; private Object[] ...

    cas 配置client 1.0 &2.0 及proxy DEMO 说明

    cas 配置client 1.0 &2.0 及proxy DEMO 说明 1 cas server 搭建 1.1 资源准备 cas server 下载 http://www.ja-sig.org/downloads/cas/cas-server-3.3.1-release.zip 1.2 解压后打开cas-server-3.3.1-release\cas-...

    Hadoop自己的Rpc框架使用Demo

    MyResponse response = proxy.call(request); System.out.println(response.getResponse()); RPC.stopProxy(proxy); } } ``` 5. **运行和测试**:编译并运行服务器端和客户端程序,你应该能看到客户端从...

    Windows+Nginx+Tomcat做负载均衡同时实现session共享Demo2

    proxy_pass_request_headers on; proxy_set_header Cookie $http_cookie; proxy_set_header Set-Cookie $http_set_cookie; proxy_pass http://redis-server:6379; } } ``` 在这个配置中,`ip_hash`指令保证了...

    微信支付H5调用支付详解(java版)

    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = ...

    java获取登录者IP和登录时间的两种实现代码详解

    ip = request.getHeader("Proxy-Client-IP"); System.out.println("Proxy-Client-IP ip: " + ip); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-...

    超级有影响力霸气的Java面试题大全文档

    redirect就是服务端根据逻辑,发送一个状态码,告诉浏览器重新去请求那个地址,一般来说浏览器会用刚才请求的所有参数重新请求,所以session,request参数都可以获取。 23、EJB与JAVA BEAN的区别?  Java Bean 是可...

    oidc-opencast-demo

    NGINX auth_request指令确保使用OpenID Connect对所有对Opencast的请求进行身份验证。 由keycloak发出的ID令牌的处理在security-jwt模块中实现。 建筑 使用附加的security-jwt模块下载并构建Opencast: make build...

    基于Spring的HttpInvoker实现改写egova_invoker.zip

    ProxyFactory.proxy 方法,生成接口的代理对象,直接调用方法客户端和服务器端的接口和实体类要一致,实体类必须是可以序列化的比如:定义Bean: public class DemoBean implements java.io.Serializable{ ...

    spring使用annotation整合dwr笔记

    &lt;listener-class&gt;org.springframework.web.context.request.RequestContextListener ``` ##### 2. 配置Spring applicationContext.xml 接下来,在`applicationContext.xml`文件中进行具体的Spring Bean配置: - ...

    spring cloud zuul修改请求url的方法

    // 原始请求路径:http://localhost:10000/demo/list/data // 修改为:/api/prefix/list/data String modifiedRequestPath = "/api/prefix" + originalRequestPath; context.put(FilterConstants.REQUEST_URI_...

    californium框架设计分析

    - **californium-proxy**: 实现CoAP到CoAP、CoAP到HTTP以及HTTP到CoAP的代理功能。 - **demo-xxx**: 示例程序,帮助用户理解和使用Californium。 3. **分层设计** - **网络层**:处理端口监听和网络数据的发送与...

    ZendFramework中文文档

    3.4. HTTP 认证适配器 3.4.1. 简介 3.4.2. 设计回顾 3.4.3. 配置选项 3.4.4. Resolvers 3.4.4.1. 文件 Resolver 3.4.5. 基本用法 4. Zend_Cache 4.1. 简介 4.2. 缓存原理 4.2.1. Zend_Cache 工厂方法 ...

    python3.6.5参考手册 chm

    urllib.request urllib.robotparser venv warnings winreg winsound xmlrpc.client zipfile zlib Optimizations Build and C API Changes Other Improvements Deprecated New Keywords Deprecated ...

Global site tag (gtag.js) - Google Analytics