I made a stand alone application for authentication against the sharepoint online, I managed to took a token and authentication cookie but, I have a problem with obtaining the Form Digest Value, when i try to obtain the Value from postman i can successfully obtain the value but, when i try to get the value from my code i relieve a java.io.IOException: Server returned HTTP response code: 411 for URL: https://XXXX.sharepoint.com/_api/contextinfo I google for the problem and check that the errors means that the Context-Length parameter is required i add such parameter with value - 0, but the result was the same.
I will be vary gratefulр if someone had meet the same problem :) Here is my code
public String getFormDigestValue(List cookies) throws IOException {
URL u = new URL("https://xxx.sharepoint.com/_api/contextinfo");
URLConnection uc = u.openConnection();
HttpURLConnection connection = (HttpURLConnection) uc;
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.addRequestProperty("Accept", "application/x-www-form-urlencoded");
connection.addRequestProperty("Content-Type","text/xml; charset=utf-8");
connection.setRequestProperty("Cookie",cookies.get(2));
connection.setRequestProperty("Cookie",cookies.get(1));
connection.setRequestProperty("Content-Length","0");
connection.setRequestProperty("Origin","xxx.sharepoint.com");
connection.setDoOutput(true);
InputStream in = connection.getInputStream();
int c;
StringBuilder sb = new StringBuilder("");
while ((c = in.read()) != -1)
sb.append((char) (c));
in.close();
String result = sb.toString();
String startTag = "<d:FormDigestValue>";
String endTag = "</d:FormDigestValue>";
if(!(result.contains(startTag)) || !(result.contains(endTag))) {
String errorMessage = "Error during getting form digest value. The form digest value is missing in the response of the request for getting form digest value.";
throw new IOException(errorMessage);
} else {
int startIndex = result.indexOf(startTag, 1);
int endIndex = result.indexOf(endTag, startIndex + 1);
String formDigestValue = result.substring(startIndex + startTag.length(), endIndex);
return formDigestValue;
}
}
I know it was asked 5 month ago, but i put answer for everyone else:
After build your post data:
StringBuilder postData = new StringBuilder();
....
....
Set post data length in "Content-Length" property:
postDataBytes = postData.toString().getBytes("UTF-8");
connection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.