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

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);
    特别是这些地方

    打赏 评论

相关推荐 更多相似问题