开发者

Java HttpClient media not supported error for POST method

开发者 https://www.devze.com 2023-01-11 21:04 出处:网络
I am trying to use httpclient and I am getting media not supported error. I want to set the headers and then get the response back which should return me headers which are tokens for authentication

I am trying to use httpclient and I am getting media not supported error. I want to set the headers and then get the response back which should return me headers which are tokens for authentication Error: Method failed: HTTP/1.1 415 Unsupported Media Type

The procedure is as follows:

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()开发者_如何学JAVA);
        }

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


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


Try without the empty space:

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

I'm not sure if Content-Type is case insensitive. This line works for me, though.

0

精彩评论

暂无评论...
验证码 换一张
取 消