小编Nit*_*hin的帖子

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

我想通过java中的代理连接到网站.这是我写的代码:

public class ConnectThroughProxy 
{
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy ip", 8080));
    public static void main(String[] args) 
    {
        try
        {
            URL url = new URL("http://www.rgagnon.com/javadetails/java-0085.html");
            URLConnection connection=url.openConnection();
            String encoded = new String(Base64.encode(new String("user_name:pass_word").getBytes()));
            connection.setDoOutput(true);
            connection.setRequestProperty("Proxy-Authorization","Basic "+encoded);
            String page="";
            String line;
            StringBuffer tmp = new StringBuffer();
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            while ((line=in.readLine()) != null)
            {
                page.concat(line + "\n");
            }
            System.out.println(page);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
Run Code Online (Sandbox Code Playgroud)

在尝试运行此代码时,它会引发以下错误:

java.lang.IllegalArgumentException:如果消息头值非法字符(或多个):基本dXNlcl9uYW1lOnBhc3Nfd29yZA ==
在sun.net.www.protocol.http.HttpURLConnection.checkMessageHeader(HttpURLConnection.java:323)
在sun.net.www.protocol. http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:2054)
在test.ConnectThroughProxy.main(ConnectThroughProxy.java:30)

任何想法怎么做?

java connection proxy

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

connection ×1

java ×1

proxy ×1