Java HTTP call to Sharepoint 2010 oData fails

左心房为你撑大大i 提交于 2020-01-01 19:57:29

问题


I am calling a Sharepoint 2010 oData service from Java which is resulting in a 400 error. I can connect to a Sharepoint 2010 list in XML format via the same code (using NTLM) successfully.

I see a related post HttpClient using both SSL encryption and NTLM authentication fails which talks of the same service (listdata.svc) and the 400 error.

Does anyone know what exact setting was used to resolve the error in the post above? Does anyone know if they are referring to the .NET Authorization Rules in IIS?

We are using IIS 7.5.

My code looks like this:

String responseText = getAuthenticatedResponse(Url, domain, userName, password);
System.out.println("response: " + responseText);

The method uses uses Java 1.6 HTTPURLConnection:

private static String getAuthenticatedResponse(
    final String urlStr, final String domain, 
    final String userName, final String password) throws IOException {

    StringBuilder response = new StringBuilder();

    Authenticator.setDefault(new Authenticator() {

        @Override
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(
                domain + "\\" + userName, password.toCharArray());
        }
    });

    URL urlRequest = new URL(urlStr);
    HttpURLConnection conn = (HttpURLConnection) urlRequest.openConnection();
    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setRequestMethod("GET");

    InputStream stream = conn.getInputStream();
    BufferedReader in = new BufferedReader(new InputStreamReader(stream));
    String str = "";
    while ((str = in.readLine()) != null) {
        response.append(str);
    }
    in.close();     

    return response.toString();
}

The error I get is :

Response Excerpt:
HTTP/1.1 400 Bad Request..Content-Type: application/xml
<message xml:lang="en-US">Media type requires a '/' character.  </message>

A similar issue is mentioned at Microsoft Social media types. Anyone run into this and know how to resolve this?

Any help would be much appreciated!

Vanita


回答1:


My colleague suggested removing content-type request header. From curl the connection to oData worked, comparing the request headers.

Curl displayed:

> GET /sites/team-sites/operations/_vti_bin/listdata.svc/UBCal?=3 HTTP/1.1
> Authorization: NTLM <redacted>
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5
> Host: hostname
> Accept: */*

Java showed the following in the trace logs:

Accept: text/html, image/gif, image/jpeg, *;q=.2, */*; q=.2

I set the Accept request header to "*/*" to the getAuthenticatedResponse method as follows:

 //Added for oData to work
conn.setRequestProperty("Accept", "*/*");

InputStream stream = conn.getInputStream();
....

This resolved the 400 error and I get the feed from Sharepoint oData service. Seems like Java set some default request headers which interfered.




回答2:


Seems like you already found a working solution, but here's an alternative using the apache httpcomponents library.

What's interesting is they don't include NTLM by default, follow -these- steps to implement it.

HttpContext localContext;

DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
    NTCredentials creds = new NTCredentials(user_name, password, domain, domain);
    httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);

    HttpHost target = new HttpHost(URL, Integer.parseInt(port), "http");
    localContext = new BasicHttpContext();

    HttpPost httppost = new HttpPost(list_name);
    httppost.setHeader("Accept", "application/json");
...


来源:https://stackoverflow.com/questions/14990869/java-http-call-to-sharepoint-2010-odata-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!