繁体   English   中英

我可以使用基本身份验证通过GET REST请求从JAVA访问SharePoint 2010吗?

[英]CAN I USE Basic Authentication to access SharePoint 2010 from JAVA with GET REST request?

我正在尝试使用REST请求从SharePoint 2010获取数据列表,但是却遇到此异常:

[err] java.io.IOException:未经授权

我的代码是:

public static String httpGet(String urlStr) throws IOException {

      URL url = new URL(urlStr);          
      String domain = "theuserdomain"; 
      String username ="myusername";
      String password = "mypassword";
      String credentials = domain+"\\"+username + ":" + password;
      String encoding = new sun.misc.BASE64Encoder().encode(credentials.getBytes());

      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setRequestProperty("Authorization", "Basic " + encoding);
      conn.connect();

      if (conn.getResponseCode() != 200) {
        throw new IOException(conn.getResponseMessage());
      }

      // Buffer the result into a string
      BufferedReader rd = new BufferedReader(
          new InputStreamReader(conn.getInputStream()));
      StringBuilder sb = new StringBuilder();
      String line;
      while ((line = rd.readLine()) != null) {
        sb.append(line);
      }
      rd.close();

      conn.disconnect();
      return sb.toString();
    }

我认为我的问题是我没有正确设置用户域...如何根据请求设置它?

已解决,使用NTLM身份验证:

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet getRequest = new HttpGet(webPage);

        NTCredentials credentials = new NTCredentials(username, password, workstation, domain);


        httpClient.getCredentialsProvider().setCredentials(new AuthScope(server,port), credentials);

        HttpResponse response = httpClient.execute(getRequest);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM