0

以下のコードで HTTP DELETE を実行しようとしましたが、常にステータス 400 のエラー メッセージを受け取りました。「httpCon.getInputStream();」を使用する必要があるかどうかはよくわかりません。

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();
        }
    }
}
4

1 に答える 1

-1

削除を行う場合、リクエストは text/xml 型であってはなりません (とにかく xmlString 変数を使用していません)。

基本的に、これが設定されていることを確認してください:

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

これの代わりに:

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

また、getInputStream() は必要ありません。

于 2011-12-24T22:34:47.940 に答える