对Sharepoint 2010 oData的Java HTTP调用失败 [英] Java HTTP call to Sharepoint 2010 oData fails

查看:277
本文介绍了对Sharepoint 2010 oData的Java HTTP调用失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Java调用了Sharepoint 2010 oData服务,导致400错误。我可以通过相同的代码(使用NTLM)成功连接到XML格式的Sharepoint 2010列表。

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.

我看到一则相关帖子使用SSL加密和NTLM身份验证的HttpClient失败会讨论相同的服务(listdata.svc)和400错误。

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.

有谁知道在上面的帖子中用什么确切的设置来解决错误?有谁知道他们是否在IIS中引用.NET授权规则?

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?

我们使用的是IIS 7.5。

We are using IIS 7.5.

我的代码如下所示:

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

该方法使用Java 1.6 HTTPURLConnection:

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

我得到的错误是:

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

Microsoft社交媒体类型。任何人遇到这个并知道如何解决这个问题?

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

推荐答案

我的同事建议删除内容类型请求标头。从curl到oData的连接工作,比较请求标头。

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

显示卷曲:

> 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在跟踪日志中显示以下内容:

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

我将Accept请求标头设置为 * / * 到getAuthenticatedResponse方法如下:

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();
....

这解决了400错误,我从Sharepoint oData获取了Feed服务。似乎Java设置了一些干扰的默认请求标头。

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

这篇关于对Sharepoint 2010 oData的Java HTTP调用失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆