Httpclient get is returning not logged in

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();
            new AuthScope(“<>”,<port>, “<realm>"),
            new UsernamePasswordCredentials(“<user>", “<password>"));

           HttpGet req = new HttpGet(“https://<>:<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);
        } else {
            final InputStream content = response.getEntity().getContent();

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?






1 answer

1 accepted


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


Hope it helps...


Hi I 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

What if you just use the SeraphAuthentication params Confluence expects DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet req = new HttpGet("https://<>:<port>:8443/confluence/rest/api/content/?title=Test+Page&spaceKey=~<user>&os_username=<user>&os_password=<password>";); final HttpResponse response = httpclient.execute(req);

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?

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

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

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)?

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.

IT confirmed that there is not wed server in-front of Confluence.

what version of HTTP commons you are using? Just very curious to understand the difference...tried with 4.3.2 trying to understand if there is an issue with redirect or something also, 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?)

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>, 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.

And the new page never shows up in confluence. I was using the same authentication in the curl as the post

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 on vanilla development version of Confluence it does nicely returns the application/json and the content. HTTP code is 200, no redirects Same goes when I run a java code provided above

thanks for all your help. I have it working now. It was a context path problem.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Dec 18, 2018 in Confluence Cloud

Happy holidays from our team to yours!

Hi Community!  2018 was filled with changes for our team, both big and small, and we've taken a lot of time to both celebrate our wins and recognize areas of improvement. One thing that we're a...

462 views 3 18
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you