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



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

[err] java.io.IOException: Unauthorized

我的代码是:

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

最新更新