About java.net. url object using proxy to access Internet Resources

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.