找不到共享点休息服务 401

sharepoint rest service 401 not found

连接到 Sharepoint 休息 Web 服务时出现未经授权的异常:

URL myURL = new URL("http://test:2014/PWA/_api/ProjectData/Projects");
            URLConnection uc = myURL.openConnection();
            HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection();
            String userCredentials = "admin:pasword";
            String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary("password".getBytes());
            uc.setRequestProperty ("Authorization", basicAuth);
            InputStream in = uc.getInputStream();

读取 url 时出现以下错误

java.io.IOException: Server returned HTTP response code: 401 for URL: http://test:2014/PWA/_api/ProjectData/Projects
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at com.jw.sharepoint.examples.XMLParser.getDocumentFromUrl(XMLParser.java:127)
    at com.jw.sharepoint.examples.XMLParser.main(XMLParser.java:27)
java.lang.NullPointerException
    at com.jw.sharepoint.examples.XMLParser.main(XMLParser.java:29)

添加请求属性和请求方法解决了问题。

    InputStream getAuthenticatedResponse(final String urlStr, final String domain,final String userName, final String password) throws IOException {


            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");
            conn.setRequestProperty("Accept", "*/*");
            return conn.getInputStream();

        }