我正在尝试向SharePoint REST界面发布请求。我正在尝试更新列值。当我使用Fiddler发布时,我能够成功地做到这一点(响应:204)。当我使用Apache HttpClient发送时,我得到400 Bad Request - Invalid Verb。
作为旁注,当我尝试将Fiddler用作此程序的代理(localhost,端口8888)时,请求成功。但是,当我再次删除代理并尝试它是不成功的。我的Java代码出错吗?
这是通过HTTP post发送JSON字符串的方式吗?
private static final String API_ENDPOINT = "/_api/web";
private static final String CONTEXT_INFO_ENDPOINT = "/_api/contextinfo";
private static final String APPLICATION_JSON = "application/json;odata=verbose";
private static final String IF_MATCH = "If-Match";
private static final String X_HTTP_Method = "X-HTTP-Method";
private static final String MERGE = "MERGE";
private static final String X_RequestDigest = "X-RequestDigest";
private static final String METADATA = "__metadata";
public static boolean setFieldValue(CloseableHttpClient httpClient, String siteURL, String serverRelativeURL, String fieldName, String fieldValue, String fieldType) {
CloseableHttpResponse response = null;
try {
URI siteURI = new URI(siteURL);
URI postURI = new URIBuilder(siteURI)
.setPath(siteURI.getPath() + API_ENDPOINT + "/GetFileByServerRelativeUrl('" + serverRelativeURL + "')/ListItemAllFields")
.build();
HttpPost postRequest = new HttpPost(postURI);
postRequest.addHeader(HttpHeaders.ACCEPT, APPLICATION_JSON );
postRequest.addHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON);
postRequest.addHeader(HttpHeaders.HOST, siteURI.getHost());
postRequest.addHeader(IF_MATCH, "*");
postRequest.addHeader(X_HTTP_Method, MERGE);
String formDigestValue = getFormDigestValue(httpClient, siteURI);
if (StringUtils.isBlank(formDigestValue)) {
logger.error("FORM DIGEST VALUE IS = " + formDigestValue);
return false;
}
postRequest.addHeader(X_RequestDigest, formDigestValue);
JSONObject type = new JSONObject();
type.put("type", fieldType);
JSONObject jsonBody = new JSONObject();
jsonBody.put(METADATA , type);
jsonBody.put(fieldName, fieldValue);
logger.debug("setFieldValue(): " + jsonBody.toString());
postRequest.setEntity(new StringEntity(jsonBody.toString()));
//String jsonString = "{'__metadata':{'type':'SP.Data.NextlabsDLItem'},'TextHeader':'Java1'}";
//JSONObject jsonObject = new JSONObject(jsonString);
//logger.debug(jsonObject);
//postRequest.setEntity(new ByteArrayEntity(jsonBody.toString().getBytes(), ContentType.APPLICATION_JSON));
logger.debug("setFieldValue(): " + postRequest.getRequestLine());
response = httpClient.execute(postRequest);
logger.debug("setFieldValue(): " + response.getStatusLine());
int status = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent()));
int cp;
StringBuilder sb = new StringBuilder();
while ((cp = bufferedReader.read()) != -1) {
sb.append((char) cp);
}
logger.debug("String Response......." + sb);
bufferedReader.close();
if (status >= 200 && status < 300) {
return true;
} else {
logger.error("setFieldValue(): " + response.getStatusLine().toString().toUpperCase());
}
} catch (URISyntaxException | IOException e) {
logger.error("setFieldValue(): " + e.getMessage(), e);
} finally {
try {
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
答案 0 :(得分:1)
很抱歉发布我自己的答案但终于找到了问题。
错误是在apache HTTPClient中启用了ExpectContinuedFlag
。
我在HTTP初始化中更改了以下内容,现在可以正常工作了!
RequestConfig requestConfig = RequestConfig.custom()
.setExpectContinueEnabled(***false***)
.setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
.setProxyPreferredAuthSchemes(Collections.singletonList(AuthSchemes.BASIC))
.build();
return HttpClients.custom()
.setConnectionManager(connManager)
.setDefaultCredentialsProvider(creds)
.setDefaultRequestConfig(requestConfig)
.build();