简体   繁体   English

我正在尝试查看其中一个网页的httpurlconnection

[英]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 : 如果您希望提取响应,则可以设置一个确定的连接超时,并在关闭连接之前按if()循环检查响应,如下所示:

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);
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试查看列表中的元素是否等于变量 - I am trying to see if an element of my list equals a variable 我正在尝试制作一个简单的计算器 - I am trying to make a calculator, a simple one 请参阅HttpURLConnection中的详细日志 - See detail log in HttpURLConnection 我正在尝试在 Eclipse 中使用 Swing 但我不断收到此错误。 见下文 - I am trying to user Swing in Eclipse but I keep getting this error. See below 不知道为什么我从httpurlconnection得到一个空的输出流 - not sure why I am getting an empty outputstream from httpurlconnection 我正在尝试查询mongodb集合以获取一个特定的id字段- - I am trying to querymongodb collection to get one particular id field - 数独-Java。 我正在尝试查看我的行是否完整。 它不能重复数字 - Sudoku - java. I am trying to see if my row is complete or not. it cannot have a number repeated 为什么在尝试查看字符串是否以特定字符结尾时会出现类型不兼容的错误? - Why am I getting an incompatible types error when trying to see if a String ends with a specific character? 我正在尝试在JFrame上查看我的时钟,但它一直在覆盖自身 - I am trying to see my clock on my JFrame, but it keeps writing over top itself 如何找到HttpURLConnection试图将我重定向到的位置? - How do I find where an HttpURLConnection is trying to redirect me to?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM