UnknownHostException:如何通过java应用程序解决UnknownHostException
我正在尝试连接到URL,并使用java代码从URL读取/下载内容。 我面临以下例外情况UnknownHostException:如何通过java应用程序解决UnknownHostException,java,jakarta-ee,http-proxy,connectionexception,Java,Jakarta Ee,Http Proxy,Connectionexception,我正在尝试连接到URL,并使用java代码从URL读取/下载内容。 我面临以下例外情况 Exception in thread "main" java.net.UnknownHostException: myURL 代码: 我是否需要在我的应用程序或代理设置中设置任何内容来解决此错误。任何建议都会很有帮助。您必须能够从运行代码的计算机到目标地址进行tracert、telnet(或ping,如果允许对目标进行ICMP)。如果这些简单操作中的任何一个都不能成功完成,那么很有可能你不被允许访问该端点
Exception in thread "main" java.net.UnknownHostException: myURL
代码:
我是否需要在我的应用程序或代理设置中设置任何内容来解决此错误。任何建议都会很有帮助。您必须能够从运行代码的计算机到目标地址进行tracert、telnet(或ping,如果允许对目标进行ICMP)。如果这些简单操作中的任何一个都不能成功完成,那么很有可能你不被允许访问该端点,防火墙阻止了访问,或者你的地址根本就错了。@kolossus-怎么做?我在命令提示符下发出了以下命令:pingmyurl,它返回ping请求找不到主机。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class GetURLContent {
public static void main(String[] args) {
URL url;
try {
// get URL content
url = new URL("https://myURL");
URLConnection conn = url.openConnection();
// open the stream and put it into BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
//save to this filename
String fileName = "C:/data/test.html";
File file = new File(fileName);
if (!file.exists()) {
file.createNewFile();
}
//use FileWriter to write file
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
while ((inputLine = br.readLine()) != null) {
bw.write(inputLine);
}
bw.close();
br.close();
System.out.println("Done");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}