2

这是我的代码

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

import com.sun.org.apache.xml.internal.security.utils.Base64;

public class webpageDisplay {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        System.getProperties().put( "proxySet", "true" ); 
        System.getProperties().put( "proxyHost", "I given proxy host" ); 
        System.getProperties().put( "proxyPort", "85" ); 
        URL url=new URL("http://www.yahoo.com");
        URLConnection uc = url.openConnection ();
        String encoded = new String
              (Base64.encode(new String("user:pass").getBytes()));
        uc.setRequestProperty("Proxy-Authorization", "Basic " + encoded);
        uc.connect();

         //String url = "http://www.yahoo.com";
            JEditorPane editor = new JEditorPane(url);
            editor.setEditable(false);
            JScrollPane pane = new JScrollPane(editor);
            JFrame f = new JFrame("HTML Demo");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(pane);
            f.setSize(800, 600);
            f.setVisible(true);


    }

}

即使我提供了代理主机和用户 ID,我仍然遇到如下错误

Exception in thread "main" java.net.UnknownHostException: www.yahoo.com
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:196)
    at java.net.Socket.connect(Socket.java:530)
    at java.net.Socket.connect(Socket.java:480)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:169)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:406)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:541)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:245)
    at sun.net.www.http.HttpClient.New(HttpClient.java:318)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:790)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:738)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:649)
    at webpageDisplay.main(webpageDisplay.java:26)

任何人都可以建议如何解决问题或我如何绕过代理

4

3 回答 3

2

您需要使用属性http.proxyHostandhttp.proxyPort而不仅仅是proxyHostand proxyPort

有关示例代码,请参阅以下问题:

如何在 Java 中使用 HTTP 代理

使用 java 中的代理代码连接到站点

于 2012-04-17T06:17:59.983 回答
0

你已经过了一半的桥。必须更改您传递 UID/pwd 的方式

您还需要一个 java.net.Authenticator 对象,必须调用其静态 setDefault() 方法来设置身份验证。浏览此处获取更多信息

http://docs.oracle.com/javase/6/docs/technotes/guides/net/http-auth.html

于 2012-04-17T06:36:59.043 回答
0
System.setProperty("http.proxyHost", "132.186.192.234");
System.setProperty("http.proxyPort", "8080");
System.setProperty("https.proxyUser", "ad001\\userid");
System.setProperty("https.proxyPassword", "sect");
于 2017-03-31T12:40:27.460 回答