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 暴力法无法解出,可能要使用dp和数学知识
  • ¥15 wpf通过绑定控件自身的值,来实现背景颜色的切换
  • ¥15 CDH6.3 运行hive -e hive -e "show databases;"报错:hive-env.sh:行24: hbase-common.jar: 权限不够
  • ¥15 SSRS制作的报表打开报错,无法正常显示网页
  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系