Java.net.UnknownHostException resolution Method

Source: Internet
Author: User
Tags base64 stringbuffer

This problem is due to the inability to find the appropriate DNS resolution, there are several ways to solve.

1. Add a row to the Windows\system32\drivers\etc\hosts

202.181.254.101 http://xxx.xxx.com

This method only solves the problem temporarily and lets the program run through, but only if you need to know the IP address of the URL.

2. Set up the agent in the program

System.setproperty ("Http.proxyhost", "myproxyserver.com"); System.setproperty ("Http.proxyport", "80");

Since Java 1.5 can also pass a Java.net.Proxy instance to the OpenConnection () method:

Proxy instance, proxy IP = 123.0.0.1 with port 8080
Proxy proxy = new Proxy (Proxy.Type.HTTP, New inetsocketaddress ("123.0.0.1", 8080));
URL url = new URL (http://www.yahoo.com);
HttpURLConnection UC = (httpurlconnection) url.openconnection (proxy);
Uc.connect ();        
String page; StringBuffer tmp = new StringBuffer ();
BufferedReader in = new BufferedReader (New InputStreamReader (Uc.getinputstream ()));
while (line = In.readline ())!= null) {   page.append (line + \ n);} SYSTEM.OUT.PRINTLN (page);

So don ' t need to set system properties.

You can use the default PROXY as defined by your networking settings.

System.setproperty ("Java.net.useSystemProxies", "true");
List L = null;
try {  L = proxyselector.getdefault (). Select (New URI ("http://www.yahoo.com")); catch (URISyntaxException e) {  e.printstacktrace ();} if (l!= null) {   
	for (Iterator iter = L.iterator (); Iter.hasnext () {      
	Java.net.Proxy Proxy = (java.net.Proxy) iter.next ();      
	SYSTEM.OUT.PRINTLN ("proxy hostname:" + proxy.type ());      
	Inetsocketaddress addr = (inetsocketaddress) proxy.address ();      
	if (addr = = null) {        System.out.println ("No Proxy");      }       
	else {        
		SYSTEM.OUT.PRINTLN ("proxy hostname:" + addr.gethostname ());        
		SYSTEM.OUT.PRINTLN ("Proxy port:" + addr.getport ());      
	}   
}
}

To bypass the PROXY,

URL url = new URL ("http://internal.server.local/"); URLConnection conn = url.openconnection (proxy.no_proxy);

Proxy and Username/password

You are might need to identify yourself to the proxy server.

One way is to use the HTTP property ' Proxy-authorization ' with a username:password base64 encoded.

System.setproperty ("Http.proxyhost", "myproxyserver.com");
System.setproperty ("Http.proxyport", "80");
URL url=new url (http://someserver/somepage);
URLConnection UC = Url.openconnection ();
String encoded = new String      (Base64.base64encode (New string ("Username:password"). GetBytes ());
Uc.setrequestproperty ("Proxy-authorization", "Basic" + encoded); Uc.connect ();

Note:for A Base64 function, this is the How-to.

The following example dumps the content of a URL but before we identify to the proxy.

Import Java.net.*;import java.io.*;
public class Urlutils {  
	public static void Main (String s[]) {    
		Urlutils.dump ("http://www.yahoo.com");    
		System.out.println ("**************");    
		Urlutils.dump ("https://www.paypal.com");    
		System.out.println ("**************");  
	}  
public static void Dump (String urlname) {    
try {      
	DataInputStream di = null;      
	FileOutputStream fo = null;      
	byte [] b = new byte[1];      PROXY      
	System.setproperty ("Http.proxyhost", "proxy.mydomain.local");      
	System.setproperty ("Http.proxyport", "80");      
	URL u = new url (urlname);      
	HttpURLConnection con = (httpurlconnection) u.openconnection ();      //It ' s not the greatest idea to use a sun.misc.* class      //Sun strongly advises not to use them since they can< c4/>//change or go away in a future release so beware.      //      
	Sun.misc.BASE64Encoder encoder = new Sun.misc.BASE64Encoder ();      
	String encodeduserpwd =         encoder.encode ("Mydomain\\myuser:mypassword". GetBytes ());      
	Con.setrequestproperty         ("Proxy-authorization", "Basic" + encodeduserpwd);      PROXY----------      
	DI = new DataInputStream (Con.getinputstream ());      
	while ( -1!= di.read (b,0,1)) {         System.out.print (new String (b));      }    
    catch (Exception e) {      e.printstacktrace ();    }  
}
}

With JDK1.2, the java.net.Authenticator can is used to send the credentials when needed.

public static void Dump (String urlname) {  try {    datainputstream di = null;    FileOutputStream fo = null;    byte [] b = new byte[1];     PROXY    system.setproperty ("Http.proxyhost", "proxy.mydomain.local");    System.setproperty ("Http.proxyport", "no");     Authenticator.setdefault (New Authenticator () {      protected passwordauthentication getpasswordauthentication () { return        New           Passwordauthentication ("Mydomain\\username", "Password". ToCharArray ());    });     URL u = new url (urlname);    HttpURLConnection con = (httpurlconnection) u.openconnection ();    DI = new DataInputStream (Con.getinputstream ());    while ( -1!= di.read (b,0,1)) {       System.out.print (new String (b));    }  catch (Exception e) {          e.printstacktrace ();  }}
Bypass a Proxy

In an intranet environment, you could need to bypass the proxy server and go directly to the HTTP server.

The Http.nonproxyhosts property indicates the hosts which should to connected too and not directly the proxy through R. The value can be a list of hosts, each seperated by a |, and in addition a wildcard character (*) can is used for match Ing.

Java.exe     -dhttp.nonproxyhosts= "*.mycompany.com|*.mycompany.local|localhost"       MyClass

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.