Recently encountered the following problems during the project process:CodeWhen:
URL. openconnection (). getinputstream ();
An exception occurred while running a connection timeout. After detailed check, I found the problem because I was on the LAN
When accessing Internet resources, I used the proxy server to access the Internet. Therefore, I modified the code to connect to Internet resources.
URL. openconnection (proxy). getinputstream ();
Compared with the above, there is only one more constructed proxy object. After testing, you can connect to the Internet. The Code is as follows:
Import Java. io. ioexception; import Java. io. inputstream; import java.net. inetaddress; import java.net. inetsocketaddress; import java.net. malformedurlexception; import java.net. proxy; import java.net. URL; /* this class is used to test the Java URL object's access to network resources through proxy */public class urlconnection {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stubstring urlstring = "http://baidu.com"; string proxyip = "1 72.20.230.5 "; int Port = 3128; try {/* construct a proxy object to apply to proxy surfing */inetsocketaddress socketaddress = new inetsocketaddress (inetaddress. getbyname (proxyip), Port); proxy = new proxy (proxy. type. HTTP, socketaddress);/* construct a URL object */URL url = new URL (urlstring);/* test whether a connection can be opened to obtain the input stream, the connection method is direct connection * // inputstream = URL. openconnection (). getinputstream ();/* the following uses a proxy for connection. You need to construct the proxy object */inputstream input = URL. openconnec Tion (proxy). getinputstream (); If (input! = NULL) {system. out. println ("connectioned") ;}} catch (malformedurlexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();}}}