POST方法不支持Java HttpClient媒体错误

时间:2010-08-12 18:27:08

标签: http httpclient

我正在尝试使用httpclient,我得到媒体不支持错误。 我想设置标题然后得到响应,它应该返回我的标题,这些标题是用于身份验证的标记 错误:方法失败:HTTP / 1.1 415不支持的媒体类型

程序如下:

public void getDocument(String url) {
    PostMethod method = new PostMethod(url);
    client.getParams().setAuthenticationPreemptive(true);

    method.setRequestHeader("User-Agent", "some header value");

    method.addRequestHeader("Header name", "some header value");
    method.addRequestHeader("Content-type", "application/xml; charset=utf-8");
    method.addRequestHeader("Content-Body", "some header value");

    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    Credentials defaultcreds = new UsernamePasswordCredentials("username", "password");
    client.getState().setCredentials(AuthScope.ANY, defaultcreds);

try {
        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
        System.err.println("Method failed: " + method.getStatusLine());
        }

catch(Exception e) {
        System.out.println("Error in getDocument() "+e);
}

2 个答案:

答案 0 :(得分:0)

postMethod = new PostMethod(URL);
postMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

答案 1 :(得分:-1)

尝试没有空格:

method.addRequestHeader("Content-Type", "application/xml;charset=UTF-8");

我不确定Content-Type是否不区分大小写。不过,这条线对我有用。