httpclient java_java使用HttpClient

public static voidsendHttpAudit(OAApplicationModule am, String type,

String entityMappingId, String param) {

System.out.println(System.currentTimeMillis());

String oaRestfulServiceUrl= null;//am.getOADBTransaction().getProfile("");

oaRestfulServiceUrl = "http://xxx.xxx.xxx.xxx:8090/api/Service/submitXXX";//RequestConfig requestConfig =//RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(1000).setSocketTimeout(5000).build();

RequestConfig requestConfig =RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(1000).build();

CloseableHttpClient httpclient= null;

CloseableHttpResponse h_response= null;try{

httpclient=HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();//httpclient = HttpClientBuilder.create().build();

HttpPost postMethod= newHttpPost(oaRestfulServiceUrl);//postMethod.setConfig(requestConfig);

postMethod.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36");

postMethod.setHeader("Referer", oaRestfulServiceUrl);

List params = new ArrayList();//200_ifaceCode 200表示为EBS系统调用RESTFUL服务。

params.add(new BasicNameValuePair("type", type));

params.add(new BasicNameValuePair("param", param));//添加参数

postMethod.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

h_response=httpclient.execute(postMethod);

HttpEntity repEntity=h_response.getEntity();int statusCode =h_response.getStatusLine().getStatusCode();if (statusCode !=HttpStatus.SC_OK) {

postMethod.abort();

procProcessError(am, entityMappingId, String.valueOf(statusCode), EntityUtils.toString(repEntity,"UTF-8") );throw new OAException("HttpClient,error status code :" +statusCode);

}

String content= EntityUtils.toString(repEntity, "UTF-8");

System.out.println(content);

JSONObject json=JSON.parseObject(content);

String retCode= json.getString("returnCode");

String retMsg= json.getString("returnMsg");

procProcessError(am, entityMappingId, retCode, retMsg );if (!"000000".equals(retCode)) {

System.out.println(" error " +retMsg );throw new OAException("接口服务处理异常" +json.getString("returnMsg"));

}else{

changeStatusInprocess(am, entityMappingId);

}

}catch(ConnectTimeoutException e) {

procProcessError(am, entityMappingId,"ConnectTimeoutException", e.getMessage() );

System.out.println(" ConnectTimeoutException " +System.currentTimeMillis());

e.printStackTrace();throw new OAException("接口服务处理异常 ConnectTimeoutException " +e.getMessage());

}catch(SocketTimeoutException e) {

procProcessError(am, entityMappingId,"SocketTimeoutException", e.getMessage() );

System.out.println(" SocketTimeoutException " +System.currentTimeMillis());

e.printStackTrace();throw new OAException("接口服务处理异常 SocketTimeoutException " +e.getMessage());

}catch(Exception e) {

procProcessError(am, entityMappingId,"Exception", e.getMessage() );

System.out.println("Exception " +System.currentTimeMillis());

e.printStackTrace();throw new OAException("接口服务处理异常 Exception " +e.getMessage());

}finally{if (h_response != null) {try{

h_response.close();

}catch(IOException e) {

e.printStackTrace();

}

}if (httpclient != null) {try{

httpclient.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值