【问题标题】:Cant connect to server, getResponseCode() returning-1无法连接到服务器,getResponseCode() 返回-1
【发布时间】:2013-11-05 21:47:20
【问题描述】:

大家好,我在下载管理器项目中工作。

我正在测试一些链接并且工作正常,但是我发现了一个问题

以下这两个链接来自emupardise,我将使用它来说明我的观点。

如果我使用此代码:

     String GameLink = "http://www.emuparadise.me/roms/get-download.php?gid=43269&token=2f91d51fcd8b90cec75193ffd592f07c&mirror_available=true";
//    String GameLink = "http://www.emuparadise.me/roms/get-download.php?gid=43270&token=a6f395885bea3bbef73d972804d2cbdb&mirror_available=true";


             try {


URL url = new URL(GameLink);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("Host" ,"www.emuparadise.me");
con.setRequestProperty("Accept" ,"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
con.setRequestProperty("Accept-Encoding" ,"gzip, deflate");
con.setRequestProperty("Referer" ,"www.emuparadise.me");
con.setRequestProperty("Connection" ,"keep-alive");



   System.out.println("Server answer :"+ con.getResponseCode());

con.connect();
InputStream is = con.getInputStream();
System.out.println(con.getURL().toString()+"\n");

is.close();

        } catch (IOException ex) {
            Logger.getLogger(Question.class.getName()).log(Level.SEVERE, null, ex);
        }

首先我得到:

“服务器答案:200”(如我所愿),但是使用我得到的第二个链接:

"Server answer :-1" 然后我得到错误:java.io.IOException: Invalid Http response

我已经检查了两个页面的标题,它们是相同的,除了两个链接都可以在 Internet 下载管理器中工作,谁能给我一个解决方案?

非常感谢您的宝贵时间

【问题讨论】:

    标签: java httpurlconnection getresponse


    【解决方案1】:

    根据javadocgetResponseCode()方法:

    “如果无法从响应中识别出任何代码(即响应不是有效的 HTTP),则返回 -1。”

    这很可能是服务器端的问题。服务器可能正在发送垃圾......或者只是关闭连接,而不是使用正确的 HTTP 响应来响应您的请求。或者它可能是这样做的中间人;即代理或反向代理。


    您可能应该尝试使用数据包监视器或其他工具来确定服务器响应中的实际内容。或者只是将其报告给站点维护人员。

    【讨论】:

    • 谢谢伙计,我发现了 javadoc 的问题
    猜你喜欢
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 1970-01-01
    • 2019-10-08
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多