有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

对Sharepoint 2010 oData的Java HTTP调用失败

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

我看到一篇相关的帖子HttpClient using both SSL encryption and NTLM authentication fails,其中谈到了相同的服务(listdata.svc)和400错误

有人知道上面帖子中的错误是用什么设置来解决的吗?有人知道他们指的是。IIS中的NET授权规则

我们正在使用IIS 7.5

我的代码如下所示:

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

该方法使用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 Social media types中也提到了类似的问题。有人遇到这个问题,知道如何解决吗

任何帮助都将不胜感激

瓦尼塔


共 (1) 个答案

  1. # 1 楼答案

    看起来您已经找到了一个可行的解决方案,但这里有一个使用apache httpcomponents库的替代方案

    有趣的是,默认情况下,它们不包括NTLM,请遵循-these-步骤来实现它

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