Redirect using HTTPS connection Response

988 Views Asked by At

Created a new HTTPS connection and executed a GET method call on the connection URL.

Instead of sending a 301 (30X) redirect I received a response with XHTML page data which inturn when handled in a browser will get submitted automatically and sent to the redirected page, but how to handle the same in java code?

Here is the response data

     <?xml version="1.0" encoding="UTF-8"?>
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
      <body onload="document.forms[0].submit()">
       <noscript><p><strong>Note:</strong> Since your browser does not support JavaScript, you must press the Continue button once to proceed.</p></noscript>
       <form action="https://localhost:8083/Login" method="POST">
          <div>
             <input type="hidden" name="RelayState" value=""/>
             <input type="hidden" name="ResponseData" value=""/>
          </div>
          <noscript>
             <div>
               <input type="submit" value="Continue"/>
             </div>
         </noscript>
       </form>
     </body>
   </html>

FYI: I just need to send a HTTP/HTTPS request to the URL in the form action attribute. I'm not planning to open the html in a browser.

1

There are 1 best solutions below

0
On

Could you provide your connection code? As a generic approach,you can set the setInstanceFollowRedirect(false) for the HttpUrlConnection, so that the redirects don't go through and you can handle them manually. Here is a good example: http://www.mkyong.com/java/java-httpurlconnection-follow-redirect-example/