how to get URL content?


try
{
	URL u = new URL("http://www.google.com");
	HttpURLConnection http_conn = (HttpURLConnection)u.openConnection();
	sun.net.www.protocol.http.HttpURLConnection h;
	http_conn.setDoInput(true);
	http_conn.setDoOutput(true);
	InputStream input = http_conn.getInputStream();
	byte b [] = new byte[1024]
;
	int pos = -1;
	String content = "";
	while((pos = input.read(b)) != -1)
	{
		content += new String(b, 0, pos);
	}
	System.out.println(content);
}
catch(Exception e)
{
	e.printStackTrace();
}

when vaadin started, the code will throws Exception


java.lang.NullPointerException
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConne
ction.java:773)
        at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection
.java:661)

if run in application or web servlet, is OK

Not really sure what you mean works and what not. I tested the code and got the expected result (google index page).

GetUrlThread.java


import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class GetUrlThread extends Thread
{
	public void run()
	{
		try
		{
		    URL u = new URL("http://www.google.com");
		    HttpURLConnection http_conn = (HttpURLConnection)u.openConnection();
		    sun.net.www.protocol.http.HttpURLConnection h;
		    http_conn.setDoInput(true);
		    http_conn.setDoOutput(true);
		    InputStream input = http_conn.getInputStream();
		    byte b [] = new byte[1024]
;
		    int pos = -1;
		    String content = "";
		    while((pos = input.read(b)) != -1)
		    {
		        content += new String(b, 0, pos);
		    }
		    System.out.println(content);
		}
		catch(Exception e)
		{
		    e.printStackTrace();
		}
	}
}

GetUrlTestApplication.java


import com.vaadin.Application;

public class GetUrlTestApplication extends Application
{
	@Override
	public void init()
	{
		GetUrlThread url_thread = new GetUrlThread();
		url_thread.start();
	}
}

start Vaadin Server, and try it again!

Run in GetUrlTestApplication Will Throws NullPointExcpetion, but if create a java Application, execute GetUrlThread Thread, The results will be correct.
The same problem will appear in the Servlet.In Vaading,What a way to get the content URL

Works fine for me (both in Tomcat and Jetty). I do not see how this problem could be related to Vaadin.

Thanks,the code is no problem.:slight_smile:
The cause is due to add the line(Mysql connection, the need to set up proxy)

ProxySelector.setDefault(null);