java - 通过 HttpURLConnection POST JSON 数据时出现 HTTP 错误 400 错误请求

标签 java json http-post httpurlconnection

我下面有这段代码,尝试在通过Oauth2.0成功验证后发布JSON数据,代码如下:

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();
}

当我运行上面的代码时出现此错误:

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 中我知道发布的 JSON 数据无效。我怀疑可能在结构中,因为根据文档它应该是这样的:

{
    "paymentMethod": {
        "type": "neteller",
        "value": "gb_gbp@neteller.com"
    },
    "transaction": {
        "merchantRefId": "20140203122501",
        "amount": 5000,
        "currency": "USD"
    },
    "verificationCode": "234124"
}

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

最佳答案

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

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");

用于写入连接的输出流(POST 的有效负载):

BufferedWriter out = 
    new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));
out.write(obj2.toString());
out.close();

上面,读取响应代码之前。

关于java - 通过 HttpURLConnection POST JSON 数据时出现 HTTP 错误 400 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26996679/

相关文章:

java - Java 获取数组列表中前 k 项的最有效方法

java - GXT FramedPanel 拖动标题

java - 在Spring上使用Jersey时从HK2收到“找不到合适的构造函数”

java - 在 JSON 请求中发送图像

ios - 如何使用 RestKit 0.20.1 发送 XML 格式的 POST 请求

java - 使用 JAVA 查询 SQL - 没有任何反应

java - Gson Instance Creator 创建了实例,但字段为空

php - 从JSON问题中提取数据

servlets - 发出长帖子请求后立即启动线程

AsyncTask 中的 android.os.NetworkOnMainThreadException