背景:调用的url外部api网络是可达的,代码我在本机和测试环境都没问题,api应该是没问题的,但是在正式环境会报错如下。httpClient版本是4.1,项目JDK是1.6
报错日志
Caused by:
java.lang.Error: Unresolved compilation problem:
at com.baosight.pscs.common.httpClientUtils.doPostJson(httpClientUtils.java:82)
代码部分
public static String doPostJson(String url, String json) throws Exception{
// 创建Httpclient对象
HttpClient httpClient = new DefaultHttpClient();
System.out.println("######################");
String resultString = "";
HttpResponse response = null;
try {
HttpPost httpPost = new HttpPost(url);
System.out.println("----------> setURL:" + url);
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
System.out.println("----------> Header");
// 创建请求内容
//StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
StringEntity postingString = new StringEntity("{\"account\":\"admin\",\"password\":\"9cbf8a4dcb8e30682b927f352d6559a0\"}");
System.out.println("----------> postingString:" + postingString);
postingString.setContentType("application/json");
//参数传入
httpPost.setEntity(postingString);
System.out.println("----------> setEntity");
response = httpClient.execute(httpPost);
System.out.println("----------> execute" + httpPost.toString());
HttpEntity entity = response.getEntity();
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println(resultString);
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
httpClient.getConnectionManager().shutdown();
}
return resultString;
}