Java 在HttpURLConnection中使用修补程序方法时出错
在这里,从过去几个小时开始,我尝试使用Java 在HttpURLConnection中使用修补程序方法时出错,java,httpurlconnection,http-status-code-400,http-patch,Java,Httpurlconnection,Http Status Code 400,Http Patch,在这里,从过去几个小时开始,我尝试使用HttpURLConnectionPUT方法更新一些资源字段。但是现在我把它改成了补丁 我能够执行GET和POST,但在Http方法PATCH中不断获取错误 请求甚至没有在邮递员中发送 这是java类: try { String serUrl = "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/tasks/255"; String authString
HttpURLConnection
PUT方法更新一些资源字段。但是现在我把它改成了补丁
我能够执行GET
和POST
,但在Http方法PATCH
中不断获取错误
请求甚至没有在邮递员中发送
这是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
我的想法是在从中获得的请求正文中发送参数
此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上使用补丁方法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中时,我们不应该发送它
我一直在尝试大量的示例程序,最后资源得到了更新。好吧,我在回答自己的问题
我只是修改了几行代码,它对我很有用。
以下是工作代码:
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
成功生成GET
和POST
请求。但是在PUT
方法中,请用代码指导我是否做错了。请参阅我上面的评论。我不知道HTTP端点是什么expects@SNSingh,400个错误的请求主要是通过url编码实现的,但是你说你已经成功地完成了GET和POST方法调用,那么我怀疑json中的参数是否丢失或者是否添加了额外的参数,你正在发送这些参数。@nandsito ok,我已经在端点添加了可用的资源,并且在添加id后输入了其中的任何一个。我可以使用POST
创建新资源,但现在无法更新现有资源。是否在任何地方记录了此服务?只有服务可以告诉我如何使用HttpUrlConnection
成功生成GET
和POST
请求。但是在PUT
方法中,请用代码指导我是否做错了。请参阅我上面的评论。我不知道HTTP端点是什么expects@SNSingh,400个错误的请求主要是通过url编码实现的,但是你说你已经成功地完成了GET和POST方法调用,那么我怀疑json中的参数是否丢失或者是否添加了额外的参数,你正在发送这些参数。@nandsito ok,我已经在端点添加了可用的资源,并且在添加id后输入了其中的任何一个。我可以使用POST
创建新资源,但现在无法更新现有资源。