代理设置未知的主机激活问题

时间:2015-06-08 08:14:22

标签: java network-programming

import java.io.*;
import java.net.*;
import java.util.*;

public class testprop {
    public static void main (String args[]) 
        throws IOException, URISyntaxException {


        // to check wether proxy working or not

        Properties p = System.getProperties();
        p.setProperty("proxySet", "true");
        p.put("https.proxyHost","proxy servername");
        p.put("https.proxyPort","80");
        System.setProperties(p);

        //if i uncomment the below line i am getting  Glib GIO error org.gnome.system.proxy is not installed
        //System.setProperty("java.net.useSystemProxies", "true");
        try {
            Proxy p1= (Proxy) ProxySelector.getDefault().select(new URI("http://google.com")).iterator().next();

            System.out.println("ProxyString"+p1.type());

            InetSocketAddress addr=(InetSocketAddress)p1.address();

            if(addr==null)
                System.out.println("no proxy");

            else{
                System.out.println("Proxy hostname" +addr.getHostName());
                System.out.println("Proxy port" +addr.getPort());

            }

            System.out.println("done");

        } catch (URISyntaxException uo){
            System.out.println("done3");
        }

        String str="google.com";

        try{
            InetAddress ip=InetAddress.getByName(str);
         System.out.println("the ip address of servicenow is"+ip);
        }catch (UnknownHostException ue){
            ue.printStackTrace();
         System.out.println("website not found");
        }    
    }
} 

我在企业网络中,所以要访问互联网,我必须通过代理。上面的代码没有为下一个连接设置代理。抛出一个未知的主机异常。 有什么办法可以实现吗? 设置代理,以便外部URL通过代理服务器传递。

输出以上代码:

输出:

ProxyStringDIRECT
no proxy
done
java.net.UnknownHostException: google.com: No address associated with hostnamewebsite not found
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:901)
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1293)
    at java.net.InetAddress.getAllByName0(InetAddress.java:1246)
    at java.net.InetAddress.getAllByName(InetAddress.java:1162)
    at java.net.InetAddress.getAllByName(InetAddress.java:1098)
    at java.net.InetAddress.getByName(InetAddress.java:1048)

在testprop.main(testprop.java:49)

0 个答案:

没有答案