简体   繁体   中英

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. It is supposed to fetch html from www.google.com and display the contents in a JFrame's JTextArea object. 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.
2. Eclipse's web browser works fine.
3. My system's browser (Mozilla Firefox) connects fine
4. I exported JSoupFetchTest into a runnable jar and tried to run it from system's level, with no effect
5. I am running Windows 7 Professional MSDN version
6. I contacted eclipse support and they concluded it is not eclipse's fault and suggested that I'm behind a proxy.
7. I contacted my ISP to see if I indeed am and they said I am not.
8. I changed my JAVA's network settings so now it connects "directly". 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"
10. Method downloadHtml(String path) stops at "is = url.openStream();" and goes on forever...

The exception I get from JSoup is:

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. 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. JSoup likely uses defaults for that, but opening the URL as a stream in Java does not.

The following program works for me. So Java and JSoup are working. It has to be some sort of local configuration problem with your network. Check your firewall, routers, gateway, and Java permissions. 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() );

   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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