簡體   English   中英

在HttpURLConnection中使用PATCH方法時出錯

[英]Getting error while using PATCH method in HttpURLConnection

從最近的幾個小時開始,我正在嘗試使用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();
}

通過覆蓋POSTHttpUrlConnection中使用PATCH方法的想法從這里得到。

我想到了在從此處獲得的請求正文中發送參數的想法。

此URL上的可用資源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"
    }]
}

我正在嘗試使用此URL上的PATCH方法update此資源的某些字段https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/tasks/255

但是每次我得到錯誤

java.io.IOException: Server returned HTTP response code: 400 for URL: https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/tasks/255
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at com.cloud.task.TaskUpdate.main(TaskUpdate.java:80)

請有人在這里幫助我解決此問題。

好吧,我在回答自己的問題。

我只修改了幾行代碼,它對我有用。 以下是工作代碼:

        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已經存在於url中時,我們不應該發送它。

我一直在嘗試使用示例程序的數量,最后資源正在更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM