gpt4 book ai didi

java - 网址连接超时

转载 作者:行者123 更新时间:2023-11-29 03:32:57 24 4
gpt4 key购买 nike

我正在尝试使用下面提到的代码访问 url

public static void main(String[] args) throws Exception {
//CookieHandler.setDefault(new CookieManager(null,CookiePolicy.ACCEPT_ALL));
// http://www.google.com/
// http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=g
**URL oracle = new URL("http://finance.yahoo.com/d/quotes.csv?s=XOM+EK+JNJ+MSFT&f=snd1t1l1ohgvwdyr");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));**
String inputLine;
while ((inputLine = in.readLine()) != null)

System.out.println(inputLine);
in.close();
}

此代码在我家工作正常,但同样的代码在办公室不工作,我收到这样的错误。

Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at com.yahoo.Connection.main(Connection.java:21)

我怀疑可能存在防火墙或代理问题,但我不确定。

有谁知道解决这个问题的方法吗?

谢谢。

最佳答案

您可能背后有一些公司代理人。

如果是,请尝试使用此代码以编程方式启用代理:

private boolean proxyEnabled = true;

public static void main(String[] args) throws Exception {
URL oracle = new URL("http://finance.yahoo.com/d/quotes.csv?s=XOM+EK+JNJ+MSFT&f=snd1t1l1ohgvwdyr");
URLConnection yc;
if(proxyEnabled) {
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("my.proxy.example.com", 3128));
yc = oracle.openConnection(proxy);
} else {
yc = oracle.openConnection();
}
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)

System.out.println(inputLine);
in.close();
}

关于java - 网址连接超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17147721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com