Httpclient get is returning not logged in
Asked by John Foster:

I am getting a error that I do not understand. It looks like credentials are accepted. Here is my code.

public static void main(String\[\] args) throws Exception  
{  
// GET section  
DefaultHttpClient httpclient = new DefaultHttpClient();  

CredentialsProvider credentials = new BasicCredentialsProvider();  
credentials.setCredentials(  
new AuthScope("\<[wiki-mine.com](http://wiki-mine.com)\>",\<port\>, "\<realm\>"),  
new UsernamePasswordCredentials("\<user\>", "\<password\>"));  

HttpGet req = new HttpGet("https://\<[wiki-mine.com](http://wiki-mine.com)\>:\<port\>:8443/conflunce/rest/api/content/?title=Test+Page\&spaceKey=\~\<user\>");  

req.addHeader("accept", "application/json");  
req.setHeader("Content-Type", "application/json");  
final HttpResponse response = httpclient.execute(req);  

final int statusCode = response.getStatusLine().getStatusCode();  
if (statusCode != 200) {  
System.out.println("Request has failed, status code: " + statusCode);  
System.exit(-1);  
} else {  
final InputStream content = response.getEntity().getContent();  
System.out.println(IOUtils.toString(content));  
IOUtils.closeQuietly(content);  
}

This is the output:

{"errorMessage":"You are not logged in. \<a href=\\"/login.action\\"\>Log in\<\\/a\> here and try again."}

Should this return the page?

thanks


Answers:
author: Alex Medved _ConfiForms_
body: Hi

2 things I have noticed...

* you have a *typo* in the url, unless your context path is really **conflunce**(and not confluence)

<!-- -->

* you have not set credentialsProvider on your httpclient  

```
httpclient.setCredentialsProvider(credentials);
```

Hope it helps...

comments: author: John Foster
body: HiI fixed the url, and added httpclient.setCredentialsProvider(credentials);. It still gives the same error. I think its is authentication problem because when I use a incorrect password it yields the same error. curl works fine and returns the page contain. Any ideas?Thanks


author: Alex Medved _ConfiForms_
body: What if you just use the SeraphAuthentication params Confluence expects<https://developer.atlassian.com/display/CONFDEV/HTTP+authentication+with+Seraph> DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet req = new HttpGet("https://\<wiki-mine.com\>:\<port\>:8443/confluence/rest/api/content/?title=Test+Page\&spaceKey=\~\<user\>\&os_username=\<user\>\&os_password=\<password\>";); final HttpResponse response = httpclient.execute(req);


author: John Foster
body: Thanks that works, but it is return html, not json. Even tho set the req header to request json with: req.addHeader("accept", "application/json"); req.setHeader("Content-Type", "application/json");Is there a way to get json content as curl returns?


author: Alex Medved _ConfiForms_
body: Just checked, it does nicely return application/json on our test server (no apache infront, just confluence tomcat)response.getEntity().getContentType()and it seems it does not matter if I set or not the above mentioned headers


author: John Foster
body: Hum, its coming back as Content-Type: text/html;charset=UTF-8 for me. Could it be a difference in server setup?


author: Alex Medved _ConfiForms_
body: which version of Confluence do you use (tried with 5.6.6 just now once again) and do you have some web server in-front of Confluence (like apache or something)?


author: John Foster
body: We are running Confluence version 5.6.4. IT is checking to see if Apache is involved, but I think not. I checked to make sure the headers where indeed being set, but it does not matter if they are set or not.


author: John Foster
body: IT confirmed that there is not wed server in-front of Confluence.


author: Alex Medved _ConfiForms_
body: what version of HTTP commons you are using? Just very curious to understand the difference...tried with 4.3.2trying to understand if there is an issue with redirect or somethingalso, what do you see in Development Tools when you request the same URL via for example Chrome browser (what is the content type, response body?)


author: John Foster
body: http commons 4.3.1. curl does not get a redirect it just sends back the correct json. firefox gets rediected to https://wiki.\<domain\>r.com:8443/dashboard.action, and displays the page. httpclient also goes to the dashboard, and returns the html for that page.also I can post a new page from curl, but when I do so from httpclient I get a 302 (found) or 200 if setRedirectStrategy(new LaxRedirectStrategy()); is called.


author: John Foster
body: And the new page never shows up in confluence. I was using the same authentication in the curl as the post


author: Alex Medved _ConfiForms_
body: frankly, I am completely lost, as if I do [http://localhost:1990/confluence/rest/api/content/?title=More+information\&spaceKey=ds\&os_username=admin\&os_password=admin](http://localhost:1990/confluence/rest/api/content/?title=More+information&spaceKey=ds&os_username=admin&os_password=admin) on vanilla development version of Confluence it does nicely returns the application/json and the content. HTTP code is 200, no redirectsSame goes when I run a java code provided above


author: John Foster
body: thanks for all your help. I have it working now. It was a context path problem.