2011-12-24 3 views
0

아래 코드로 HTTP 삭제를 시도했지만 항상 Status 400 오류 메시지가 나타났습니다. 잘못 입력했는지 알고 싶었습니다. 나는 내가 "httpCon.getInputStream();"을 사용할 필요가 있는지 너무 확신하지 못한다.HTTP DELETE는 항상 HTTP/400 오류 메시지를 반환합니다.

import java.io.IOException; 
import java.net.HttpURLConnection; 
import java.net.URL; 

public class test { 
     public static void main(String[] args) throws IOException { 

     URL url = new URL("http://testurl.svc/web/testrequests/"+893488); 

     StringBuffer xmlString = new StringBuffer(); 

     xmlString.append("<TestRequest>"); 
     xmlString.append("<DateRequested>2011-12-23</DateRequested>"); 
     xmlString.append("<ID>893488</ID>"); 
     xmlString.append("<Version>1</Version>"); 
     xmlString.append("<TestID>19104</TestID>"); 
     xmlString.append("<ProjectName>LTS</ProjectName>"); 
     xmlString.append("<RequestedBy>ktmq331</RequestedBy>"); 
     xmlString.append("<SampleNumber>SN1033646000</SampleNumber>"); 
     xmlString.append("<Status>Requested</Status>"); 
     xmlString.append("</TestRequest>"); 

     System.out.println("xmlString :" + xmlString.toString()); 

     System.out.println("URL : " + url); 

     try { 
      System.out.println("URL : " + url); 
      HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); 
      httpCon.setDoOutput(true); 
      httpCon.setRequestProperty("Content-Type", "text/xml"); 
      httpCon.setRequestMethod("DELETE"); 
      httpCon.connect(); 
      httpCon.getInputStream(); 
      int responseCode = httpCon.getResponseCode(); 
      System.out.print("Response Code: "+responseCode); 
      String responseString = httpCon.getResponseMessage(); 
      System.out.print("Response Message: "+responseString); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

답변

-1

삭제를 수행하는 경우 요청의 유형이 text/xml이 아니어야합니다 (어쨌든 xmlString 변수를 사용하지 않아도됩니다). 대신이의

httpCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 

:

기본적으로이 설정되어 있는지 확인

httpCon.setRequestProperty("Content-Type", "text/xml"); 

을 그리고 당신은()는 getInputStream 필요하지 않습니다.

+0

여전히 같은 오류 메시지가 나타납니다. HHTP DELETE를 호출하는 방법이 올바른지 확인할 수 있습니까? – Winz

+0

내가해야 할 일은 http DELETE 메서드를 사용하여 다음 URL "http : //testurl.svc/web/testrequests/"+893488 "에있는 값을 삭제하는 것입니다. – Winz

관련 문제