简体   繁体   中英

I am trying to see httpurlconnection for one of the webpage

while trying to check the broken links in selenium for one of the webpage i am not able to get the response message. i get the below error.

Can you please help me out what is the problem Code used is :

HttpURLConnection connection = (HttpURLConnection) new URL(activelinks.get(j).getAttribute("href")).openConnection();
connection.connect();
Serializable response = connection.getResponseMessage();
connection.disconnect();
System.out.println(activelinks.get(j).getAttribute("href")+"----->"+response);

Error is :

Exception in thread "main" java.net.ProtocolException: Server redirected too many  times (20)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at java.net.HttpURLConnection.getResponseCode(Unknown Source)
    at java.net.HttpURLConnection.getResponseMessage(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseMessage(Unknown Source)
    at Testng.amazonn.amazontestcase.main(amazontestcase.java:45)

If you are looking to extract the response you may like to set a definite connection Timeout and check the response in a if() loop before closing the connection as follows :

HttpURLConnection connection = (HttpURLConnection) new URL(activelinks.get(j).getAttribute("href")).openConnection();
connection.setConnectTimeout(5000);
connection.connect();
if (connection.getResponseCode() == 200) 
{
    System.out.println(activelinks.get(j).getAttribute("href")+"----->" + connection.getResponseMessage());
}

if (connection.getResponseCode() == httpUrlConnect.HTTP_NOT_FOUND) 
{
    System.out.println(activelinks.get(j).getAttribute("href")+"----->" + connection.HTTP_NOT_FOUND);
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM