Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取HTTP错误400错误请求-尝试更新SharePoint字段时URL无效_Java_Sharepoint_Httpclient_Http Status Code 400 - Fatal编程技术网

Java 获取HTTP错误400错误请求-尝试更新SharePoint字段时URL无效

Java 获取HTTP错误400错误请求-尝试更新SharePoint字段时URL无效,java,sharepoint,httpclient,http-status-code-400,Java,Sharepoint,Httpclient,Http Status Code 400,我试图使用ApacheHttpClient更新Sharepoint文档库中的一个字段,但不断收到HTTP错误400-无效URL。代码和输出如下所述。请告知发生这种情况的原因 公共静态布尔setFieldValue(CloseableHttpClient httpClient、String siteURL、String serverRelativeURL、String fieldName、String fieldValue){ CloseableHttpResponse httpResponse

我试图使用ApacheHttpClient更新Sharepoint文档库中的一个字段,但不断收到HTTP错误400-无效URL。代码和输出如下所述。请告知发生这种情况的原因

公共静态布尔setFieldValue(CloseableHttpClient httpClient、String siteURL、String serverRelativeURL、String fieldName、String fieldValue){
CloseableHttpResponse httpResponse=null;
试一试{
URI siteURI=新URI(siteURL);
URI postURI=新的URIBuilder(siteURI)
.setPath(siteURI.getPath()+“/_api/web/GetFileByServerRelativeUrl(“+serverRelativeURL+”)/ListItemAllFields”)
.build();
HttpPost HttpPost=新的HttpPost(postURI);
字符串formDigestValue=getFormDigestValue(httpClient,Postori);
if(StringUtils.isBlank(formDigestValue)){
logger.error(“表单摘要值为=“+formDigestValue”);
返回false;
}
addHeader(ACCEPT,APPLICATION_JSON);
addHeader(内容类型,应用程序类型);
addHeader(X_HTTP_方法,MERGE);
httpPost.addHeader(如果匹配,则标点符号为星号);
addHeader(X_请求_摘要,formDigestValue);
JSONObject JSONObject=新的JSONObject();
jsonObject.put(字段名、字段值);
put(“_元数据”,new jsonObject().put(“type”,“SP.Data.PuneetsLibraryItem”));
调试(jsonObject);
setEntity(新的StringEntity(jsonObject.toString());
trace(httpPost.getRequestLine());
trace(httpPost.getURI());
httpResponse=httpClient.execute(httpPost);
HttpEntity entity=httpResponse.getEntity();
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(entity.getContent());
if(bufferedReader!=null){
int-cp;
StringBuilder sb=新的StringBuilder();
而((cp=bufferedReader.read())!=-1){
sb.追加((char)cp);
}
logger.debug(“字符串响应…”+sb);
bufferedReader.close();
}
logger.debug(“响应…”+entity.getContent());
trace(httpResponse.getStatusLine().getReasonPhrase());
int status=httpResponse.getStatusLine().getStatusCode();
如果(状态>=200&&status<300){
返回true;
}否则{
logger.error(“错误:+httpResponse.getStatusLine().toString().toUpperCase());
}
}捕获(URISyntaxException | IOException e){
logger.error(e.getMessage(),e);
}最后{
试一试{
if(httpResponse!=null){
httpResponse.close();
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
返回false;
}
控制台日志:

13:57:44.089 [main] DEBUG com.nextlabs.smartclassifier.sharepoint.SharePointUtil - POST http://sp2013w2k12r2/_api/contextinfo HTTP/1.1
13:57:45.417 [main] TRACE com.nextlabs.smartclassifier.sharepoint.SharePointUtil - HTTP/1.1 200 OK
13:57:45.417 [main] DEBUG com.nextlabs.smartclassifier.sharepoint.SharePointUtil - {"TestField":"Puneet","__metadata":{"type":"SP.Data.PuneetsLibraryItem"}}
13:57:45.433 [main] TRACE com.nextlabs.smartclassifier.sharepoint.SharePointUtil - POST http://sp2013w2k12r2/_api/web/GetFileByServerRelativeUrl('/PuneetsLibrary/Three.docx')/ListItemAllFields HTTP/1.1
13:57:45.433 [main] TRACE com.nextlabs.smartclassifier.sharepoint.SharePointUtil - http://sp2013w2k12r2/_api/web/GetFileByServerRelativeUrl('/PuneetsLibrary/Three.docx')/ListItemAllFields
13:57:45.448 [main] DEBUG com.nextlabs.smartclassifier.sharepoint.SharePointUtil - String Response.......<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid URL</h2>
<hr><p>HTTP Error 400. The request URL is invalid.</p>
</BODY></HTML>

13:57:45.448 [main] DEBUG com.nextlabs.smartclassifier.sharepoint.SharePointUtil - Response.......org.apache.http.conn.EofSensorInputStream@199bd995
13:57:45.448 [main] TRACE com.nextlabs.smartclassifier.sharepoint.SharePointUtil - Bad Request
13:57:45.448 [main] ERROR com.nextlabs.smartclassifier.sharepoint.SharePointUtil - ERROR: HTTP/1.1 400 BAD REQUEST
13:57:45.448 [main] INFO  com.nextlabs.smartclassifier.util.HTTPClientUtil - Trying to close HTTP Connections
13:57:45.448 [main] INFO  com.nextlabs.smartclassifier.util.HTTPClientUtil - HTTP Connections closed successfully.
13:57:44.089[main]调试com.nextlabs.smartclassifier.sharepoint.SharePointUtil-POSThttp://sp2013w2k12r2/_api/contextinfo HTTP/1.1
13:57:45.417[main]TRACE com.nextlabs.smartclassifier.sharepoint.SharePointUtil-HTTP/1.1 200 OK
13:57:45.417[main]DEBUG com.nextlabs.smartclassifier.sharepoint.SharePointUtil-{“测试字段”:“Puneet”,“_u元数据”:{“类型”:“SP.Data.PuneetsLibraryItem”}
13:57:45.433[main]TRACE com.nextlabs.smartclassifier.sharepoint.SharePointUtil-POSThttp://sp2013w2k12r2/_api/web/GetFileByServerRelativeUrl('/PuneetsLibrary/Three.docx')/ListItemAllFields HTTP/1.1
13:57:45.433[main]TRACE com.nextlabs.smartclassifier.sharepoint.SharePointUtil-http://sp2013w2k12r2/_api/web/GetFileByServerRelativeUrl('/PuneetsLibrary/Three.docx')/ListItemAllFields
13:57:45.448[main]调试com.nextlabs.smartclassifier.sharepoint.SharePointUtil-字符串响应。。。。。。。
错误的请求
错误的请求-无效的URL
HTTP错误400。请求URL无效

13:57:45.448[main]DEBUG com.nextlabs.smartclassifier.sharepoint.SharePointUtil-Response…….org.apache.http.conn。EofSensorInputStream@199bd995 13:57:45.448[main]TRACE com.nextlabs.smartclassifier.sharepoint.SharePointUtil-请求错误 13:57:45.448[main]错误com.nextlabs.smartclassifier.sharepoint.SharePointUtil-错误:HTTP/1.1 400错误请求 13:57:45.448[main]INFO com.nextlabs.smartclassifier.util.HTTPClientUtil-正在尝试关闭HTTP连接 13:57:45.448[main]INFO com.nextlabs.smartclassifier.util.HTTPClientUtil-HTTP连接已成功关闭。
我意识到我的错误,所以我发布了一个答案。HttpClient正在请求中发送一个
expect
头字段,它不应该发送该字段。此外,
X-Request-Digest
必须被
X-Request-Digest
替换

关闭expect标头的方法如下所示:

defaultRequestConfig = RequestConfig.custom()
        .setExpectContinueEnabled(false)
        .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
        .setProxyPreferredAuthSchemes(Collections.singletonList(AuthSchemes.BASIC))
        .build();

httpclient = HttpClients.custom()
        .setConnectionManager(httpClientConnectionManager)
        .setDefaultCredentialsProvider(credentialsProvider)
        .setDefaultRequestConfig(defaultRequestConfig)
        //.setConnectionTimeToLive(1, TimeUnit.MINUTES)
        .build();

你能试着打印吗url@pushpendraURL打印在控制台日志中。您是否又看到了一个答案?我的服务器相对URL是正确的。当我用get而不是post尝试相同的url时,一切正常。因此,post请求肯定有问题,而不是url
defaultRequestConfig = RequestConfig.custom()
        .setExpectContinueEnabled(false)
        .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
        .setProxyPreferredAuthSchemes(Collections.singletonList(AuthSchemes.BASIC))
        .build();

httpclient = HttpClients.custom()
        .setConnectionManager(httpClientConnectionManager)
        .setDefaultCredentialsProvider(credentialsProvider)
        .setDefaultRequestConfig(defaultRequestConfig)
        //.setConnectionTimeToLive(1, TimeUnit.MINUTES)
        .build();