在 HttpURLConnection 中使用 PATCH 方法时出错
Getting error while using PATCH method in HttpURLConnection
最近几个小时,我正在尝试使用 HttpURLConnection
PUT 方法更新某些资源字段。但现在我将其更改为 PATCH.
我能够执行 GET
和 POST
,但在 Http 方法中 PATCH
不断出错。
请求甚至没有在 POSTMAN
中发送。
这是java
class:
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 获得的 POST
在 HttpUrlConnection
中使用 PATCH
方法的想法。
我想到了在来自 here 的请求正文中发送参数。
这里urlhttps://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
此资源的某些字段,在此 PATCH 方法 url 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.
中时,我们不应该发送它
我一直在尝试大量的示例程序,终于更新了资源。
最近几个小时,我正在尝试使用 HttpURLConnection
PUT 方法更新某些资源字段。但现在我将其更改为 PATCH.
我能够执行 GET
和 POST
,但在 Http 方法中 PATCH
不断出错。
请求甚至没有在 POSTMAN
中发送。
这是java
class:
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 获得的 POST
在 HttpUrlConnection
中使用 PATCH
方法的想法。
我想到了在来自 here 的请求正文中发送参数。
这里urlhttps://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
此资源的某些字段,在此 PATCH 方法 url 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.
我一直在尝试大量的示例程序,终于更新了资源。