我正在尝试使用HttpURLConnection创建一个简单的http请求。我收到此错误,连接超时。我正在使用intellij IDEA,并正确设置代理。检查设置中的连接,表示连接成功。可能出了什么问题?这是我的代码。
java.net.ConnectException: Connection timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
at sun.net.www.http.HttpClient.New(HttpClient.java:306)
at sun.net.www.http.HttpClient.New(HttpClient.java:323)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:860)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:801)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:726)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1049)
at Hello.main(Hello.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Process finished with exit code 0
错误如下
import java.io.*;
import java.net.*;
import java.util.Properties;
/**
* Created by admin on 22/8/15.
*/
public class Hello {
// This method should be removed in production
static void setProxy(){
Properties systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost","lotus");
systemProperties.setProperty("http.proxyPort", "8080");
}
public static void main(String [] args)
{
try
{
setProxy();
URL url = new URL("http://www.google.com");
URLConnection urlConnection = url.openConnection();
HttpURLConnection connection = null;
if(urlConnection instanceof HttpURLConnection)
{
connection = (HttpURLConnection) urlConnection;
}
else
{
System.out.println("Please enter an HTTP URL.");
return;
}
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String urlString = "";
String current;
while((current = in.readLine()) != null)
{
urlString += current;
}
System.out.println(urlString);
}catch(IOException e)
{
e.printStackTrace();
}
}
}
ps:在代码中使用系统属性确实有效。但这对我的目的来说是不可行的。对此有任何解决方法吗? 我不想编写如下代码,即从生产中删除一些代码。以下工作正常。
code
答案 0 :(得分:0)
创建两个具有相同名称的属性文件(proxy.properties)。使用包含以下内容的那个:
http.proxyHost=lotus
http.proxyPort=8080
...用于开发和包含以下内容的那个:
http.proxyHost=
http.proxyPort=
应该运到prod。
然后在你上课:
static Proxy getProxy() throws IOException {
Properties proxyProperties = new Properties();
InputStream inputStream = null;
try {
inputStream = Hello.getClass().getClassLoader().getResourceAsStream("proxy.properties");
proxyProperties.load(inputStream);
} finally {
if(inputStream != null) {
try {
inputStream.close();
} catch(IOException ioe) {
}
}
}
String proxyHost = proxyProperties.getProperty("http.proxyHost");
if(proxyHost != null && proxyHost.length() > 0)) {
String proxyPort = proxyProperties.getProperty("http.proxyPort");
SocketAddress addr = new InetSocketAddress(proxyHost, Integer.parseInt(proxyPort));
return new Proxy(Proxy.Type.HTTP, addr);
}
return null;
}
然后在你的主要方法中:
...
URL url = new URL("http://www.google.com");
URLConnection urlConnection;
Proxy = getProxy();
if(proxy != null) {
urlConnection = url.openConnection(proxy);
} else {
urlConnection = url.openConnection();
HttpURLConnection connection = null;
...