Java Http〜URLConnection响应代码-1 [英] Java Http~URLConnection response code -1

查看:135
本文介绍了Java Http〜URLConnection响应代码-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从Http服务器(nginx)下载Java文件

Trying to download a file in Java from a Http server (nginx)

java尝试下载的链接与浏览器和下载中的链接完全相同,但是java响应:

The exact same link java is attempting to download works in the browser and downloads, but java responds with:

java.io.IOException: <URL WOULD BE HERE> returned response code -1
    at com.atlauncher.data.Downloadable.getConnection(Downloadable.java:149)
    at com.atlauncher.data.Downloadable.getFilesize(Downloadable.java:85)
    at com.atlauncher.workers.InstanceInstaller.configurePack(InstanceInstaller.java:1134)
    at com.atlauncher.workers.InstanceInstaller.doInBackground(InstanceInstaller.java:1399)
    at com.atlauncher.workers.InstanceInstaller.doInBackground(InstanceInstaller.java:59)
    at javax.swing.SwingWorker$1.call(SwingWorker.java:296)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at javax.swing.SwingWorker.run(SwingWorker.java:335)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)

下载代码:

this.connection = (HttpURLConnection) new URL(this.url).openConnection();
    this.connection.setUseCaches(false);
    this.connection.setDefaultUseCaches(false);
    this.connection.setConnectTimeout(9000);
    //this.connection.setRequestProperty("Accept-Encoding", "gzip");
    this.connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36");
    this.connection.setRequestProperty("Cache-Control", "no-store,max-age=0,no-cache");
    this.connection.setRequestProperty("Expires", "0");
    this.connection.setRequestProperty("Pragma", "no-cache");
    this.connection.connect();
    if (this.connection.getResponseCode() / 100 != 2) {
        System.out.println(this.connection);
        throw new IOException(this.url + " returned response code "
                + this.connection.getResponseCode());
    }

任何想法为什么会这样?奇怪的是,完全相同的URL在浏览器中起作用.而且完全相同的代码可以从同一服务器和目录下载不同文件...

Any ideas why this is occurring? It's strange that the exact same url works in the browser. And the exact same code works downloading different files from the same server, and directory...

推荐答案

在我使用自己的HTTP服务器实现的一种情况下,发生了这种情况.在浏览器中运行时,它可以很好地工作,但是创建HttpURLConnection可以给出Invalid Http response,响应代码为-1.

This has happened to me on one occasion where I was using my own implementation of an HTTP server. When running in browsers, it would work fine but making an HttpURLConnection would give the Invalid Http response and response code would be -1.

问题是HttpURLConnection严格按照格式查找标头

The problem was that the HttpURLConnection looks for header strictly in the format

HTTP/1.1 200 OK

但是我的自定义服务器只提供了一个

but my custom server was giving just an

HTTP 200 OK

我将版本更改为1.1后就可以使用.因此,请使用cURL检查您的响应,如下所示:

As soon as I changed the version to 1.1, it worked. So check your response using cURL as follows:

curl -v -I URL_HERE

希望有帮助!

这篇关于Java Http〜URLConnection响应代码-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆