如何使用Apache HTTPClient 4.5发送带有JSON参数的POST。获得415不支持的媒体类型

时间:2016-10-19 18:21:37

标签: java apache apache-httpclient-4.x

我尝试使用一组JSON参数向我的服务器发送POST请求(使用Apache HTTPClient 4.5)。我已经关注了一些SO问题,但遇到了问题。

当我使用javascript控制台发送请求时,它可以正常工作!像这样:

//Using JS console, I send a POST request and it works.
$.post('/createConfigData', {
    "tailSign": "A7ALE",
    "active": "Y"
});
//Get back 201

当我使用 Apache HTTPClient 4.5 尝试执行上述操作时,我会返回 415不支持的媒体类型

HttpPost httppost = new HttpPost('/createConfigData');
String jsondata = "{\"tailSign\": \"A7ALE\",\"active\": \"Y\"}";

StringEntity jsonparam = new StringEntity(jsondata);
jsonparam.setContentType("application/json;charset=utf-8");
jsonparam.setChunked(true);

httppost.addHeader("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
httppost.setEntity(jsonparam);

httpresponse = httpclient.execute(target, httppost);

我从工作请求获得的数据是:

  • 请求标题:
    • 主持人:" mysite.com"
    • User-Agent:" Mozilla / 5.0(X11; Ubuntu; Linux x86_64; rv:25.0)Gecko / 20100101 Firefox / 25.0"
    • 接受:"*/*"
    • 接受语言:" en-US,en; q = 0.5"
    • 接受编码:" gzip,deflate"
    • 内容类型:" application / x-www-form-urlencoded;字符集= UTF-8"
    • Referer:" mysite.com/index.html"
    • 内容长度:" 288"
    • Cookie:" JSESSIONID = ..."
    • 代理授权:"基本......"
    • 连接:" keep-alive"
    • Pragma:" no-cache"
    • 缓存控制:" no-cache"
  • 响应标题:
    • 年龄:" 0"
    • 连接:" Keep-Alive"
    • 内容类型:" application / json; charset = UTF-8"
    • 日期:" 2016年10月19日星期三17:57:34 GMT"
    • 服务器:" Apache-Coyote / 1.1"
    • 转移编码:" chunked"

我对设置内容类型的位置感到有点困惑,无论是实体还是 httppost

=======================

更多测试用例

httpost' 内容类型设置为 json ,可以 400

HttpPost httppost = new HttpPost('/createConfigData');
String jsondata = "{\"tailSign\": \"A7ALE\",\"active\": \"Y\"}";

StringEntity jsonparam = new StringEntity(jsondata);
jsonparam.setChunked(true);

httppost.addHeader("content-type", "application/json;charset=UTF-8");
httppost.setEntity(jsonparam);

httpresponse = httpclient.execute(target, httppost);

httpost 内容类型设置为 x-www-form-urlencoded ,可以 415

HttpPost httppost = new HttpPost('/createConfigData');
String jsondata = "{\"tailSign\": \"A7ALE\",\"active\": \"Y\"}";

StringEntity jsonparam = new StringEntity(jsondata);
jsonparam.setChunked(true);

httppost.addHeader("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
httppost.setEntity(jsonparam);

httpresponse = httpclient.execute(target, httppost);

我也尝试添加这一行:

httppost.addHeader("Accept", "*/*");

==================

使用 wireshark 我能够发现有一条错误消息与400错误请求一起出现!这只是我的JSON不正确。

HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 19 Oct 2016 21:33:25 GMT
Proxy-Connection: Keep-Alive
Connection: Keep-Alive

{"code":400,"message":"Field ConfigName is Null, Invalid Value for Seat Count, Missing counts for DSU, ICMT, SVDU, TPMU, Login user details not found, Please enter valid lruData","fleetData":{"airlineData":null,"dimAircraftJson":null,"configData":null}}

1 个答案:

答案 0 :(得分:2)

您获得 415不支持的媒体类型,因为您已将内容类型标头覆盖为 application / x-www-form-urlencoded;字符集= UTF-8 即可。只需将其更改为:

HttpPost httppost = new HttpPost('/createConfigData');
String jsondata = "{\"tailSign\": \"A7ALE\",\"active\": \"Y\"}";
StringEntity jsonparam = new StringEntity(jsondata);
jsonparam.setChunked(true);

httppost.addHeader("content-type", "application/json;charset=UTF-8");
httppost.setEntity(jsonparam);

httpresponse = httpclient.execute(target, httppost);