加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – intellij Idea中的连接超时错误

发布时间:2020-12-15 02:09:18 所属栏目:Java 来源:网络整理
导读:我正在尝试使用HttpURLConnection创建一个简单的http请求.我收到此错误,连接超时.我正在使用intellij IDEA,并正确设置代理.检查设置中的连接,表示连接成功.可能出了什么问题?这是我的代码. import java.io.*;import java.net.*;/** * Created by admin on 2
我正在尝试使用HttpURLConnection创建一个简单的http请求.我收到此错误,连接超时.我正在使用intellij IDEA,并正确设置代理.检查设置中的连接,表示连接成功.可能出了什么问题?这是我的代码.

import java.io.*;
import java.net.*;

/**
 * Created by admin on 22/8/15.
 */
public class Hello {
    public static void main(String [] args)
    {
        try
        {
            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();
        }
    }
}

错误如下

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

ps:在代码中使用系统属性确实有效.但这对我的目的来说是不可行的.对此有任何解决方法吗?
我不想编写如下代码,即从生产中删除一些代码.以下工作正常.

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();
        }
    }
}

解决方法

创建两个具有相同名称的属性文件(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;
...

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读