vigoolexiong 2020-05-03 09:58 采纳率: 100%
浏览 722
已采纳

HTTPClient提交失败,fiddler提交成功是什么原因?

模拟提交数据到其他公司api接口,代码如下,提交到后台后返回:{"error":"Cannot read property 'enable' of null"},服务器500错误 ;通过fiddler抓取提交的数据,在fiddler重新提交又是正常的,求助高手啊。
HTTP/1.1 500 Internal Server Error
Content-Type: application/json; charset=utf-8
Content-Length: 49
Date: Sun, 03 May 2020 01:29:59 GMT

X-XSS-Protection: 1;mode=block
Strict-Transport-Security: max-age=31536000

{"error":"Cannot read property 'enable' of null"}

fiddler不做任何修改重新提交:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 63
Date: Sun, 03 May 2020 01:31:47 GMT
X-XSS-Protection: 1;mode=block
Strict-Transport-Security: max-age=31536000

{"result":"5eae1f037eab9e653ce863b8","errcode":0,"errmsg":"OK"}

代码如下:
public static HttpResponse sendAddOrderJsonPost(String url, String json, UserAuth userAuth) {
HttpResponse response = null;
HttpPost post = new HttpPost(url);
post.setConfig(HttpsGlobal.config);
post.setHeader("Host", userAuth.getHost());
post.setHeader("Connection", "keep-alive");
post.setHeader("Accept", "application/json, text/plain, /");
post.setHeader("Origin", userAuth.getOrigin);
post.setHeader("User-Agent", userAuth.getUser_agent());
post.setHeader("Content-Type", "application/json; charset=UTF-8");
post.setHeader("Referer", url);
post.setHeader("Accept-Encoding", "gzip, deflate");
post.setHeader("Accept-Language", "zh-cn-#hans");
post.setHeader("Cookie", userAuth.getCookies());
try {
StringEntity requestEntity = new StringEntity(json, "UTF-8");
post.setEntity(requestEntity);
response = HttpsGlobal.client.execute(post);
} catch (SocketTimeoutException e1) {
HttpsGlobal.logger.error("SocketTimeoutException!");
} catch (ConnectTimeoutException e2) {
HttpsGlobal.logger.error("ConnectTimeoutException");
} catch (ConnectException e3) {
HttpsGlobal.logger.error("ConnectException");
} catch (IOException e4) {
HttpsGlobal.logger.error("IOException");
} catch (Exception e5) {
HttpsGlobal.logger.error("Exception");
}
return response;
}


  • 写回答

1条回答 默认 最新

  • threenewbee 2020-05-03 10:32
    关注

    抓包对比下
    特别是cookie user-agent postdata referer 中文编码这些
    post.setHeader("User-Agent", userAuth.getUser_agent());

    post.setHeader("Cookie", userAuth.getCookies());

    StringEntity requestEntity = new StringEntity(json, "UTF-8");
    post.setEntity(requestEntity);
    特别是这些地方

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 请选择合适的标签 6月16日

悬赏问题

  • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧