简体   繁体   English

JAVA应用程序和ECLIPSE无法连接到互联网

[英]JAVA apps and ECLIPSE can't connect to internet

Ok so I wrote a piece of code testing ability of my java to connect to internet. 好的,所以我写了一段Java代码测试功能来连接互联网。 It is supposed to fetch html from www.google.com and display the contents in a JFrame's JTextArea object. 它应该从www.google.com获取html并将其内容显示在JFrame的JTextArea对象中。 Here's the code, so you can have clear picture: 这是代码,因此您可以清晰看到图片:

import java.awt.Color;
import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class JSoupFetchTest extends JFrame{
    String userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0";
    boolean jsoupcond = true;
    String address = "http://www.google.com";
    JTextArea text;
    public JSoupFetchTest(){
        text = new JTextArea();
        text.setPreferredSize(new Dimension(500, 500));
        text.setBackground(Color.BLACK);
        text.setForeground(Color.WHITE);
        text.setVisible(true);
        text.setLineWrap(true);     
        this.add(text);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.pack();        
        gogo();
    }

    private void gogo() {
        if(jsoupcond){
            text.setText(text.getText() +"\nstart...");

            try {
                text.setText(text.getText() +"\nConnecting to " +address+ "...");   
                Document doc = Jsoup.connect(address).userAgent(userAgent).get();
                text.setText(text.getText() +"\nConverting page document into text");
                String s = doc.toString();
                text.setText(text.getText() +"\nText: \n" +s);
                System.out.println();
            } catch (Exception e) {            
                text.setText(text.getText() +"\n" +e.toString());
                e.printStackTrace();
            }
            text.setText(text.getText() +"\nEnd.");
        }
        String html = downloadHtml(address);
        text.setText(text.getText() +"\nDownloading HTML...");
        text.setText(text.getText() +"\nHTML:");
        text.setText(text.getText() +"\n" +html);
    }

    private String downloadHtml(String path) {
        text.setText(text.getText() +"\ndownloadHtml entry point...");
        InputStream is = null;
        try {
            text.setText(text.getText() +"\ntry block entered...");
            String result = "";
            String line;

            URL url = new URL(path);
            text.setText(text.getText() +"\nabout to open url stream...");
            is = url.openStream();  // throws an IOException
            text.setText(text.getText() +"\nurl stream opened...");
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            text.setText(text.getText() +"\nstarting to read lines...");
            while ((line = br.readLine()) != null) {
                result += line;
            }
            text.setText(text.getText() +"\nreading lines finished...");
            return result;
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            try {
                if (is != null) is.close();
            } catch (IOException ioe) { }
        }
        return "";
    }

    public static void main(String[] args) {
        new JSoupFetchTest();       
    }
}

I should also add that: 我还应该补充一点:
1. My eclipse (cause that's what I'm using) can't connect to marketplace nor can't fetch updates. 1.我的日食(因为这就是我正在使用的东西)无法连接到市场,也无法获取更新。
2. Eclipse's web browser works fine. 2. Eclipse的Web浏览器工作正常。
3. My system's browser (Mozilla Firefox) connects fine 3.我的系统的浏览器(Mozilla Firefox)正常连接
4. I exported JSoupFetchTest into a runnable jar and tried to run it from system's level, with no effect 4.我将JSoupFetchTest导出到一个可运行的jar中,并尝试从系统级别运行它,但没有任何效果
5. I am running Windows 7 Professional MSDN version 5.我正在运行Windows 7 Professional MSDN版本
6. I contacted eclipse support and they concluded it is not eclipse's fault and suggested that I'm behind a proxy. 6.我联系了eclipse支持人员,他们认为这不是eclipse的错,并建议我支持代理。
7. I contacted my ISP to see if I indeed am and they said I am not. 7.我联系了我的ISP,看我是否确实是,他们说我不是。
8. I changed my JAVA's network settings so now it connects "directly". 8.我更改了JAVA的网络设置,因此现在它可以“直接”连接。 Before the setting was "use browser settings" and it didn't work either. 之前的设置是“使用浏览器设置”,它也不起作用。
9. My eclipse's Window -> Preferences -> General -> Network Connections active provider is set to "Native", I also tried "Direct" 9.我的日食的窗口->首选项->常规->网络连接活动提供程序设置为“本机”,我也尝试过“直接”
10. Method downloadHtml(String path) stops at "is = url.openStream();" 10.方法downloadHtml(String path)在“ is = url.openStream();”处停止 and goes on forever... 并永远持续下去...

The exception I get from JSoup is: 我从JSoup得到的异常是:

java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:150)
    at java.net.SocketInputStream.read(SocketInputStream.java:121)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:703)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1534)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:453)
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:434)
    at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:181)
    at org.jsoup.helper.HttpConnection.get(HttpConnection.java:170)
    at JSoupFetchTest.gogo(JSoupFetchTest.java:42)
    at JSoupFetchTest.<init>(JSoupFetchTest.java:32)
    at JSoupFetchTest.main(JSoupFetchTest.java:92)

I also tried to set JSoup.connect's timeout to infinity. 我还尝试将JSoup.connect的超时设置为无穷大。 Then it goes on forever. 然后它永远持续下去。

Before you guys say that my question is a duplicate, or delegate me to other, external possible solutions to my problem, believe me - either the question is mine or I was there - I browse internet in search for solution for weeks now and I feel like pulling my hair out... 在你们说我的问题是重复的,或者委派我解决我的问题的其他外部可能解决方案之前,请相信我-问题是我的,还是我当时在-我浏览互联网以寻找解决方案已有数周之久,所以我觉得像拔头发一样
Please help if you can cause it prevents me from installing stuff in my eclipse and from developing anything else than stand alone apps... 如果您会导致它阻止我在日食中安装东西以及开发除独立应用程序以外的任何其他内容,请提供帮助...

You need a socket number after the URL -- "http:/www.google.com:80" works. URL后面需要一个套接字号-“ http:/www.google.com:80”起作用。 JSoup likely uses defaults for that, but opening the URL as a stream in Java does not. JSoup可能为此使用默认值,但在Java中不以URL流形式打开URL。

The following program works for me. 以下程序对我有用。 So Java and JSoup are working. 因此Java和JSoup都在工作。 It has to be some sort of local configuration problem with your network. 网络必须是某种本地配置问题。 Check your firewall, routers, gateway, and Java permissions. 检查您的防火墙,路由器,网关和Java权限。 Do a clean rebuild of your project. 彻底重建项目。 Etc. Comment out lines until it does work and then put the lines back one at a time until you find the problem. 等等,注释掉所有行,直到它起作用为止,然后将每一行重新放回一行,直到发现问题为止。 Etc. 等等。

package stuff;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;


public class SocketTest
{

   public static void main( String[] args ) throws Exception
   {
      URL url = new URL( "http://www.google.com" );
      URLConnection sock = url.openConnection();
      InputStream ins = sock.getInputStream();
      BufferedReader reader = new BufferedReader( new InputStreamReader(ins, "UTF-8" ) );
      for( String line; (line = reader.readLine()) != null; ) {
         System.out.println( line );
      }
      ins.close();

      Document doc = Jsoup.connect( "http://www.google.com" ).get();
      System.out.println( doc.toString() );

      String userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0";
      Document doc2 = Jsoup.connect( "http://www.google.com" ).userAgent(userAgent).get();
      System.out.println( doc2.toString() );

   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM