ITPub博客

首页 > > > 同样一段代码,在本地运行好使,在云服务器运行不好使

同样一段代码,在本地运行好使,在云服务器运行不好使

作者:llittle 时间:2019-10-09 17:08:03 0 删除 编辑

这是一个发送post请求的工具类,在本地可以正常获取到返回的数据,上传到服务器后,无法获取到数据,而且日志记录显示未发生异常

public static JSONObject doPost(String url,JSONObject json){
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
JSONObject response = null;
try {
StringEntity s = new StringEntity(json.toString());
s.setContentEncoding("UTF-8");
s.setContentType("application/json");//发送json数据需要设置contentType
post.setEntity(s);
HttpResponse res = client.execute(post);
if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
String result = EntityUtils.toString(res.getEntity());// 返回json格式:
logger.info("result:"+result);
response = JSON.parseObject(result);
}
} catch (Exception e) {
logger.error("error:"+e.getMessage());
e.printStackTrace();
}
logger.info("请求结束");
return response;
}


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/813/viewspace-3136764/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-12

  • 博文量
    448
  • 访问量
    4369