0
votes

I am trying to verify if a content exists in SharePoint using SharePoint content urls in Java such as

http://sharepointportal/Lists/News/Attachments/11/mylink.pdf

Below is code for same. Here i am trying to get response code for a content URL by creating a HTTP connection from URL. If i get HTTP 200, it means content exists in SharePoint.

String fileUrl = "http://sharepointportal/Lists/News/Attachments/11/mylink.pdf";
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection connection = (HttpURLConnection) new URL(fileUrl).openConnection();
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("HEAD");
if(connection.getResponseCode() == HttpURLConnection.HTTP_OK)
{
  fileExists = true;
}

But while invoking this URL from Java, i am getting HTTP 401 - Authentication Required Error.

I also do have username & password for SharePoint with me. Is there any way i can use username/password details in URL to authenticate with SharePoint and chexk whether content exists?

1
From my knowledge, for a java code to interact with any system, the system needs to either provide access to itself through one of the following: (1) Java APIs (2) Web Services. Does your sharepoint server do that ?james
How about looking for this problem online ? Eg. social.msdn.microsoft.com/Forums/sharepoint/en-US/…james
The URL for content is from Sharepoint & accessed using Java.Finn
Then please put your java code here. If possible, add a dummy sharepoint server link in the code.james
Added java code for creating HTTP connection from content URL with dummy sharepoint content link.Finn

1 Answers

0
votes

I got it working using HTTPClient API with NTLM credentials.

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGetRequest = new  HttpGet(fileUrl);
org.apache.http.auth.Credentials credentials = new org.apache.http.auth.NTCredentials(username,password, workstation, domain);
httpClient.getCredentialsProvider().setCredentials(SharePoint_URL, SharePoint_Port),credentials);
HttpResponse response = httpClient.execute(httpGetRequest);
StatusLine status = response.getStatusLine();