在HttpURLConnection

时间:2017-08-03 11:27:04

标签: java httpurlconnection http-status-code-400 http-patch

在过去的几个小时里,我尝试使用HttpURLConnection PUT 方法更新某些资源字段。但现在我已将其更改为 PATCH

我可以执行GETPOST,但在Http方法中PATCH不断出错。

该请求甚至没有在POSTMAN中发送。

这是java类:

try {
    String serUrl = "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/tasks/255";
    String authString = user + ":" + password;
    byte[] authEncBytes = Base64.getEncoder().encode(authString.getBytes());
    String authStringEnc = new String(authEncBytes);

    URL url = new URL(serUrl); //Enter URL here
    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
    httpURLConnection.setDoInput(true);
    httpURLConnection.setDoOutput(true);
    httpURLConnection.setRequestProperty("X-HTTP-Method-Override", "PATCH");
    httpURLConnection.setRequestMethod("POST");
    httpURLConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
    httpURLConnection.setRequestProperty("Content-Type", "application/json");

    httpURLConnection.connect();

    String inputJson = "{   \"id\":" + 255 + "," +
        "\"assignedToAccount\": {" +
        "     \"id\":" + 233 +
        " }," +
        " \"name\":\"" + "task2_checking34" + "\"," +
        " \"serviceSettings\":{" +
        "     \"incident\":{" +
        "         \"id\":" + 380 +
        "     }" +
        " }" +
        "}";
    OutputStreamWriter osw = new OutputStreamWriter(httpURLConnection.getOutputStream());
    osw.write(inputJson);
    osw.flush();
    osw.close();

    BufferedReader br = new BufferedReader(new InputStreamReader(
        (httpURLConnection.getInputStream())));

    String output;
    StringBuffer bfr = new StringBuffer();
    String res = "";
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
        bfr.append(output);
    }
    res = bfr.toString();

} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

通过覆盖从here获取的PATCH,在HttpUrlConnection中使用POST方法的想法。

我想到了从here获取的请求正文中发送参数。

此网址https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/tasks/的可用资源类似于

{
    "id": 253,
    "lookupName": "task_quality34",
    "createdTime": "2017-08-03T05:34:34Z",
    "updatedTime": "2017-08-03T05:34:34Z",
    "links": [{
        "rel": "canonical",
        "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/tasks/253"
    }]
}, {
    "id": 255,
    "lookupName": "task_quality-test12",
    "createdTime": "2017-08-03T05:48:26Z",
    "updatedTime": "2017-08-03T05:48:26Z",
    "links": [{
        "rel": "canonical",
        "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/tasks/255"
    }]
}

我正在尝试使用此网址update

但每次我都会收到错误

https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/tasks/255

请有人帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

好吧,我回答了自己的问题。

我刚刚修改了几行代码,它对我有用。 以下是工作代码:

        try {
            URL url = new URL(serUrl); //Enter URL here
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
            httpURLConnection.setRequestProperty("Accept", "application/json");
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            httpURLConnection.setRequestProperty("X-HTTP-Method-Override", "PATCH");
            httpURLConnection.connect();

            DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
            wr.write(inputJson.getBytes());
            wr.flush();
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (httpURLConnection.getInputStream())));


            StringBuffer bfr = new StringBuffer();
            String output = "";
            String res = "";

            while ((output = br.readLine()) != null) {
                bfr.append(output);
            }
            resCode = httpURLConnection.getResponseCode();
//            System.out.println("response code = "+resCode);
            if (resCode != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + resCode +"\n"
                        +bfr.toString());
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

在变量inputJson中,我们不应该发送参数id,而它已经在网址中。

我一直在尝试使用示例程序的数量,最后资源也在不断更新。