Java,"URI不能为空"简单HTTP客户端上的错误 [英] Java, "URI can't be null" error on simple HTTP client
问题描述
我正在尝试编写一个简单的HTTP客户端,该客户端具有给定的绝对HTTP路径,可以执行一个简单的HTTP GET请求并将其内容打印到标准输出中.
I'm trying to write a simple HTTP client that, given an absolute HTTP path, performs a simple HTTP GET request and prints out the content to standard output.
但是,我尝试加载的任何页面上都出现以下错误:
However, I'm getting the following error on any page I try to load:
java.lang.IllegalArgumentException: URI can't be null.
at sun.net.spi.DefaultProxySelector.select(DefaultProxySelector.java:141)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:926)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:850)
at HTTrack.main(HTTrack.java:68)
引发异常的代码是:
try
{
System.out.println("We will attempt to read " + getfilepath(args[0]) + " from " + getbasesite(args[0]));
URL serverConnect = new URL("http", getbasesite(args[0]), getfilepath(args[0]));
con = (HttpURLConnection)serverConnect.openConnection();
con.setRequestMethod("GET");
con.setDoOutput(true);
con.setReadTimeout(10000);
System.out.println("Attempting to connect to " + getbasesite(args[0]) + " on port 80...");
System.out.println("URL = " + con.getURL());
con.connect();
//System.out.println("Connection succeeded! Attempting to read remote file " + getfilepath(args[0]));
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = "";
while((line = br.readLine()) != null)
{
System.out.println(line);
}
}
引发异常(HTTrack.java:68)
的行是con.connect()
行,而函数getbasesite
和getfilepath
分别返回URL的服务器主机名和远程文件路径.
The line that is raising the exception (HTTrack.java:68)
is the con.connect()
line, and the functions getbasesite
and getfilepath
return the server host name and remote file paths of a URL, respectively.
例如,如果传递字符串http://www.somesite.com/somepage.html
,则getbasesite
将返回"www.somesite.com"
,而getfilepath
将返回"/somepage.html"
.我知道HttpURLConnection
正确传递了这些值,因为当我调用getURL
时,它将返回我期望的值:http://www.somesite.com/somepage.html
For example if passed the string http://www.somesite.com/somepage.html
, getbasesite
will return "www.somesite.com"
and getfilepath
will return "/somepage.html"
. I know the HttpURLConnection
is being passed these values correctly because when I call getURL
it returns what I expect: http://www.somesite.com/somepage.html
我对可能导致此错误的原因有所了解-我测试了HttpURLConnection
类的确从带有System.out.println("URL = " + con.getURL());
行的构造函数参数中获取了正确的URL,所以我不确定为什么尝试连接失败,并显示"URI不能为空"的错误.
I'm stuck as to what might be causing this error - I've tested that the HttpURLConnection
class indeed gets the correct URL out of the constructor arguments with the line System.out.println("URL = " + con.getURL());
, so I'm not sure why it's failing the attempts to connect with the error that the "URI can't be null".
推荐答案
尝试删除您的con.setDoOutput(true);
或将其设置为false
.该行告诉您将使用连接来写输出,但是稍后您将通过调用con.getInputStream()
从流中读取.
Try removing your con.setDoOutput(true);
or setting it to false
. That line is telling your connection that you are going to be using it to write output, but then later on you are reading from the stream by calling con.getInputStream()
.
这篇关于Java,"URI不能为空"简单HTTP客户端上的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!