簡體   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