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日

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!