【问题标题】:HTTP DELETE always returns a HTTP/400 error messageHTTP DELETE 始终返回 HTTP/400 错误消息
【发布时间】:2011-12-25 06:34:47
【问题描述】:

我尝试通过以下代码执行 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();
        }
    }
}

【问题讨论】:

    标签: java xmlhttprequest http-status-code-400 http-delete


    【解决方案1】:

    如果您正在执行删除,则请求不应是 text/xml 类型(您甚至没有使用 xmlString 变量)。

    基本上确保已设置:

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

    而不是这个:

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

    而且你不需要 getInputStream()。

    【讨论】:

    • 我仍然收到同样的错误信息..请您验证调用HHTP DELETE的方法是否正确?
    • 我需要做的是使用http DELETE方法删除以下URL“testurl.svc/web/testrequests/"+893488”中存在的值。
    猜你喜欢
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    相关资源
    最近更新 更多