Java Http POST with basic authorization and redirect

authorizationhttpjava

The program does a http post with basic authorization just fine but when the post is complete the page is redirected to a success page. The redirect failes due to 401 authorization failed.

        final URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestProperty("Authorization", "basic " +base64);
        wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();
        rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

The line

rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

fails due to 401 authorization failed…

I have also tried adding

conn.setRequestProperty("Authorization", "basic " +base64);

after

wr.flush();

I get the error of "Already connected". Evidently the authorization that I set doesn't follow over to the redirect. Any solutions to this problem is greatly appreciated.

Best Answer