2017-08-03 318 views
0

这里从最近几个小时,我试图用HttpURLConnectionPUT方法更新一些资源字段。但现在我已将其更改为修补程序在HttpURLConnection中使用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(); 
} 

使用HttpUrlConnectionPATCH方法通过重写POSThere得到的想法。

我得到了从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) 

请别人帮忙请在这里解决这个问题。

+0

这个服务记录在任何地方吗?只有服务可以告诉如何构建正确的请求 – nandsito

+0

我已经成功地使用'HttpUrlConnection'构建'GET'和'POST'请求​​。但在'PUT'方法中,请指导我是否做错了任何错误。 – Shambhu

+0

参考我上面的评论。我不知道什么HTTP端点预计 – nandsito

回答

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

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

相关问题