1 HttpClient client = HttpClientUtils.getConnection();//得到client
HttpUriRequest post = HttpClientUtils.getRequestMethodNew(map, chekUserLoginUrl, "post",authorization,time);
//通过此接口拼接报文
2 //拼接报文 +参数设置。
private static PoolingHttpClientConnectionManager connectionManager = null;
private static HttpClientBuilder httpBulder = null;
private static RequestConfig requestConfig = null;
private static int MAXCONNECTION = 20;
private static int DEFAULTMAXCONNECTION = 5;
private static String IP = "*";
private static int PORT = 0;
static {
//设置http的状态参数
requestConfig = RequestConfig.custom()
.setSocketTimeout(5000)
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.build();
HttpHost target = new HttpHost(IP, PORT);
connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(MAXCONNECTION);
connectionManager.setDefaultMaxPerRoute(DEFAULTMAXCONNECTION);
connectionManager.setMaxPerRoute(new HttpRoute(target), 20);
httpBulder = HttpClients.custom();
httpBulder.setConnectionManager(connectionManager);
}
public static CloseableHttpClient getConnection() {
CloseableHttpClient httpClient = httpBulder.build();
httpClient = httpBulder.build();
return httpClient;
}
public static HttpUriRequest getRequestMethodNew(Map<String, String> map, String url, String method,String authorization,String time) throws Exception, IOException {
List<NameValuePair> params = new ArrayList<NameValuePair>();Set<Map.Entry<String, String>> entrySet = map.entrySet();
for (Map.Entry<String, String> e : entrySet) {
String name = e.getKey();
String value = e.getValue();
NameValuePair pair = new BasicNameValuePair(name, value);
params.add(pair);
}
url.replaceAll(" ","");
HttpUriRequest reqMethod = null;
if ("post".equals(method)) {
//报文体方法1
/* reqMethod = RequestBuilder.post().setUri(url).addParameters(params.toArray(new BasicNameValuePair[params.size()]))
.setConfig(requestConfig).build();*/
System.out.println("开始写psot报文 主体和头部");
/* 体 */
JSONObject jsonParam = new JSONObject();
JSONObject jsonParam1 = new JSONObject();
/* List<BasicNameValuePair> formParams = new ArrayList<BasicNameValuePair>();
formParams.add(new BasicNameValuePair("username", map.get("username")));
formParams.add(new BasicNameValuePair("password",map.get("password")));
formParams.add(new BasicNameValuePair("appId", map.get("appId")));
formParams.add(new BasicNameValuePair("userIP",map.get("userIP")));
formParams.add(new BasicNameValuePair("userClient", map.get("userClient")));*/
//报文体方法2 其中方法的body是以json的格式。
jsonParam.put("username", map.get("username"));jsonParam.put("password",map.get("password"));
jsonParam.put("appId", map.get("appId"));
jsonParam.put("userIP",map.get("userIP"));
jsonParam.put("userClient", map.get("userClient"));
StringEntity entity = new StringEntity(jsonParam.toString(),"utf-8");//非json格式直接放入formParams
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
reqMethod =RequestBuilder.post().setUri(url).setEntity(entity).setConfig(requestConfig).build();
/* 头 */
/* Map<String,String> aHeaders = new HashMap<String,String>();
JSONArray array = new JSONArray();
List<Headers> aHeaders2 = new ArrayList<Headers>();*/
/* aHeaders.put("User-Agent",map.get("userClient"));
aHeaders.put("Accept-Language","en-US,en;q=0.5");
aHeaders.put("Accept-Encoding","gzip, deflate");
aHeaders.put("Content-Type","application/json; charset=utf-8");
aHeaders.put("Authorization",authorization);
aHeaders.put("x-idm-date",time);
array.add(aHeaders);
reqMethod.setHeader((org.apache.http.Header) array);*/
reqMethod.setHeader("User-Agent", map.get("userClient"));
reqMethod.setHeader("accept", "application/json, text/plain, */*");
reqMethod.setHeader("Accept-Language", "en-US,en;q=0.5");
reqMethod.setHeader("Accept-Encoding", "gzip, deflate");
reqMethod.setHeader("Content-Type", "application/json; charset=utf-8");
reqMethod.setHeader("connection", "Keep-Alive");
reqMethod.setHeader("Authorization", authorization);// 数字签名
reqMethod.setHeader("x-idm-date", time);// 调用时间
System.out.println("头部结束------");
} else if ("get".equals(method)) {
reqMethod = RequestBuilder.get().setUri(url)
.addParameters(params.toArray(new BasicNameValuePair[params.size()]))
.setConfig(requestConfig).build();
}
return reqMethod;
}
3 接上面的1-----HttpResponse response = client.execute(post);
System.out.println("得到response----------"+response);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
String obj = EntityUtils.toString(entity, "utf-8");
if(StringUtils.isEmpty(obj)){
throw new CheckUserTPException("系统错误:身份平台验证失败1");
}else{
CheckUserLoginPio user = JSONObject.parseObject(obj, CheckUserLoginPio.class);
if(StringUtils.isEmpty(user.getCode()) || StringUtils.isEmpty(user.getMessage())){
throw new CheckUserTPException("测:未返回报文");
}else
if(!user.getCode().equals("0")){
String message =user.getMessage();
throw new CheckUserTPException(message+"dddddd");
}
else{
isCheck = true;
}
}
} else {
//throw new CheckUserTPException("系统错误:身份平台验证失败2");
//post.abort();
int aString = response.getStatusLine().getStatusCode();
throw new CheckUserTPException(aString+"状态码"+ HttpStatus.SC_OK);
}
return isCheck;