首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过HttpURLConnection POSTing JSON数据时出现HTTP错误400错误请求

通过HttpURLConnection POSTing JSON数据时出现HTTP错误400错误请求
EN

Stack Overflow用户
提问于 2014-11-18 22:29:03
回答 1查看 12.2K关注 0票数 1

我有下面的代码,试图在通过Oauth2.0成功认证后发布JSON数据,代码如下:

代码语言:javascript
复制
try {            
    URL url = new URL ("https://test.api.neteller.com/v1/oauth2/token?grant_type=client_credentials");
    String test = "QUFBQlNhVlFoUjhlTVhPQjowLm9aV3g2Rkhwd1dIQUZnXzRaMDZwOUoyRWtzaXVHMHEzd2xrVFF4MkRVM28uN0FzUDE4cmZXU0dxVDdqWE1NUGhzSFY4cHU0";
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);
    connection.setRequestProperty  ("Authorization", "Basic " + test);
    String accessToken = "";
    [... get the access token ...]

    String urlnew = "https://test.api.neteller.com/v1/transferIn";
    URL obj = new URL(urlnew);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

    //add request header
    con.setRequestMethod("POST");
    con.setDoOutput(true);          
    con.setRequestProperty  ("Authorization", "Bearer " + accessToken);
    con.setRequestProperty("Content-Type", "application/json");

    // Send post request            
    JSONObject obj2 = new JSONObject();
    obj2.put("paymentMethod", "");
    obj2.put("type", "neteller");
    obj2.put("value", "netellertest_GBP@neteller.com");
    obj2.put("transaction", "");
    obj2.put("merchantRefId", "20140203122501");
    obj2.put("amount",  (5000));
    obj2.put("currency", "USD");
    obj2.put("verificationCode", "411392");
    System.out.print(obj2);             

    int responseCode = con.getResponseCode();
    System.out.println("Response Code : " + responseCode);

    [... parse response ...]
} catch(Exception e) {
    e.printStackTrace();
}

当我运行上面的代码时,我得到了这个错误:

代码语言:javascript
复制
java.io.IOException: Server returned HTTP response code: 400 for URL: https://test.api.neteller.com/v1/transferIn
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at example.demo.main(demo.java:71)
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://test.api.neteller.com/v1/transferIn
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at java.net.HttpURLConnection.getResponseCode(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
    at example.demo.main(demo.java:65)

从错误状态400我知道post JSON数据无效。我怀疑可能在结构中,因为根据文档,它应该是这样的:

代码语言:javascript
复制
{
    "paymentMethod": {
        "type": "neteller",
        "value": "gb_gbp@neteller.com"
    },
    "transaction": {
        "merchantRefId": "20140203122501",
        "amount": 5000,
        "currency": "USD"
    },
    "verificationCode": "234124"
}

那么,如何更改JSON参数,使其遵循文档中的结构呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-18 23:51:14

对于所需的数据方案,如下所示:

代码语言:javascript
复制
JSONObject obj2 = new JSONObject();
JSONObject paymentMethod = new JSONObject();
paymentMethod.put("type", "neteller");
paymentMethod.put("value", "netellertest_GBP@neteller.com");
obj2.put("paymentMethod", paymentMethod);
JSONObject transaction = new JSONObject();
transaction.put("merchantRefId", "20140203122501");
transaction.put("amount",  (5000));
transaction.put("currency", "USD");
obj2.put("transaction", transaction);
obj2.put("verificationCode", "411392");

用于写入连接的outputStream (POST的有效负载):

代码语言:javascript
复制
BufferedWriter out = 
    new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));
out.write(obj2.toString());
out.close();

以上内容,在阅读responseCode之前。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26996679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档