使用Java http客户端使用Sharepoint 2010 REST API发布数据

时间:2014-11-24 16:05:15

标签: java api rest sharepoint

我正在尝试使用Java客户端将SharePoint列表项发布到SharePoint 2010列表。 我可以使用下面的代码读取SP列表项,但我不知道如何写入SharePoint。 对于post操作,我收到http 401错误。 任何指导将不胜感激。

GET操作:

String url = "https://organization.company.com/sites/Ateam/_vti_bin/ListData.svc/TableLoadInfo";        DefaultHttpClient client = new DefaultHttpClient();

        NTCredentials credentials = new NTCredentials(username, password, hostname, domain);
        AuthScope scope = new AuthScope(hostname, 443);

        SSLContext sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(null, new TrustManager[] { trustmanager }, null);

        SSLSocketFactory sf = new SSLSocketFactory(sslcontext,              SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        Scheme https = new Scheme("https", 443, sf);

        client.getConnectionManager().getSchemeRegistry().register(https);
        client.getCredentialsProvider().setCredentials(scope, credentials);

        HttpGet request = new HttpGet(url);
        //request.addHeader("Accept", "application/xml");
        request.addHeader("Accept", "application/json;odata=verbose");

        HttpResponse response = client.execute(request);

POST操作:

String url =“https://organization.company.com/sites/Ateam/_vti_bin/ListData.svc/TableLoadInfo”;

DefaultHttpClient client = new DefaultHttpClient();

        NTCredentials credentials = new NTCredentials(username, password, hostname, domain);
        AuthScope scope = new AuthScope(hostname, 443);

        SSLContext sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(null, new TrustManager[] { trustmanager }, null);

        SSLSocketFactory sf = new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        Scheme https = new Scheme("https", 443, sf);

        client.getConnectionManager().getSchemeRegistry().register(https);
        client.getCredentialsProvider().setCredentials(scope, credentials);

        // POST a data
        HttpPost request = new HttpPost(url);

                    request.addHeader("Accept", "application/json;odata=verbose");
        request.addHeader("Content-type", "application/json;odata=verbose");
        // request.addHeader("X-RequestDigest", FormDigestValue);
        request.addHeader("X-HTTP-Method", "POST");
        request.addHeader("If-Match", "*");

        JSONStringer json = (JSONStringer) new JSONStringer().object().key("Table_Name").value("TableName 1")
                .key("Load_Frequency").value("Weekly").key("Cycle").value("CURRENT").endObject();


        StringEntity se = new StringEntity(json.toString());
        request.setEntity(se);

                    HttpResponse response = client.execute(request);

1 个答案:

答案 0 :(得分:0)

将数据发布到SharePoint时,必须在标题内添加“FormDigestValue”。

请注意,您已注释“request.addHeader("X-RequestDigest", FormDigestValue);”行。

这就是你得到401错误的原因。

首先,您必须从当前站点获取“FormDigestValue”值。 然后在标题内添加该值。 以下代码显示了获取FormDigestValue的方法。

            String digestqueryURL = serverurl + subsite + "/" + "_api/contextinfo";
            HttpPost httpPost = new HttpPost(digestquery);
            httpPost.addHeader("Accept", "application/json;odata=verbose");
            httpPost.addHeader("X-ClientService-ClientTag", "SDK-JAVA");
            HttpResponse response = httpClient.execute(httpPost);

            byte[] content = EntityUtils.toByteArray(response.getEntity());

            String jsonString = new String(content, "UTF-8");
            JSONObject json = new JSONObject(jsonString);

            String FormDigestValue = json.getJSONObject("d")
                    .getJSONObject("GetContextWebInformation")
                    .getString("FormDigestValue");

然后在执行后期操作时将FormDigestValue添加到标题中。

request.addHeader("X-RequestDigest", FormDigestValue);