/**
* 通过HttpClient调用服务
*
* @param url 路径
* data json数据
* @return
*/
//post请求方法
public String sendItsmTask(String url, String data) throws Exception{
System.out.println("进入发送itsm方法 url:"+ url + "data:" +data);
String isSuccess = "success";
HttpPost post = null;
try {
HttpClient httpClient = new DefaultHttpClient();
// 设置超时时间
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 2000);
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 2000);
post = new HttpPost(url);
// 构造消息头
post.setHeader("Content-type", "application/json; charset=utf-8");
post.setHeader("Connection", "Close");
// 构建消息实体
StringEntity entity = new StringEntity(data, Charset.forName("UTF-8"));
entity.setContentEncoding("UTF-8");
// 发送Json格式的数据请求
entity.setContentType("application/json");
post.setEntity(entity);
HttpResponse response = httpClient.execute(post);
// 检验返回码
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode != HttpStatus.SC_OK){
log.info("请求出错: "+statusCode);
isSuccess = "error";
}else{
String retCode = "";
// 返回码中包含retCode
// 找响应信息
for(Header header : response.getAllHeaders()){
System.out.println("这是个什么神奇的东西"+header.getName() +":" + header.getValue());
if("retcode".equals(header.getName())){
retCode = header.getValue();
}
}
if(IAS_SUCCESS != retCode ){
// 日志打印
log.info("调用全网接口下发工单 返回状态异常响应码为: "+retCode);
isSuccess = "error";
}
}
} catch (Exception e) {
e.printStackTrace();
isSuccess = "error";
}finally{
if(post != null){
try {
post.releaseConnection();
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
return isSuccess;
}