这里从最近几个小时,我试图用HttpURLConnection
PUT方法更新一些资源字段。但现在我已将其更改为修补程序。在HttpURLConnection中使用PATCH方法时出现错误
我能够执行GET
和POST
,但在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();
}
使用HttpUrlConnection
PATCH
方法通过重写POST
从here得到的想法。
我得到了从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
这个资源的一些领域,使用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)
请别人帮忙请在这里解决这个问题。
这个服务记录在任何地方吗?只有服务可以告诉如何构建正确的请求 – nandsito
我已经成功地使用'HttpUrlConnection'构建'GET'和'POST'请求。但在'PUT'方法中,请指导我是否做错了任何错误。 – Shambhu
参考我上面的评论。我不知道什么HTTP端点预计 – nandsito