使用Java中的代理代码连接到网站 [英] Connect to a site using proxy code in java

查看:91
本文介绍了使用Java中的代理代码连接到网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to connect to as site through proxy in java. This is the code which I have written:

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

尝试运行此代码时会抛出以下错误:

While trying to run this code it throws the following error:


java.lang.IllegalArgumentException:邮件标头值中的非法字符:Basic dXNlcl9uYW1lOnBhc3Nfd29yZA ==

at sun.net.www.protocol.http.HttpURLConnection.checkMessageHeader(HttpURLConnection.java:323)

at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:2054)< br>
at test.ConnectThroughProxy.main(ConnectThroughProxy.java30)

java.lang.IllegalArgumentException: Illegal character(s) in message header value: Basic dXNlcl9uYW1lOnBhc3Nfd29yZA==
at sun.net.www.protocol.http.HttpURLConnection.checkMessageHeader(HttpURLConnection.java:323)
at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:2054)
at test.ConnectThroughProxy.main(ConnectThroughProxy.java:30)

任何想法如何做? / p>

Any Idea how to do it?

推荐答案

如果你只是试图通过HTTP代理服务器发出HTTP请求,你不需要去这么多努力。这里有一个写法: http://java.sun .com / javase / 6 / docs / technotes / guides / net / proxies.html

If you're just trying to make HTTP requests through an HTTP proxy server, you shouldn't need to go to this much effort. There's a writeup here: http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html

但它基本上归结为只设置http.proxyHost和http.proxyPort环境属性,在命令行或代码中:

But it basically boils down to just setting the http.proxyHost and http.proxyPort environment properties, either on the command line, or in code:


// Set the http proxy to webcache.mydomain.com:8080
System.setProperty("http.proxyHost", "webcache.mydomain.com");
System.setProperty("http.proxyPort", "8080");

// Next connection will be through proxy.
URL url = new URL("http://java.sun.com/");
InputStream in = url.openStream();

// Now, let's 'unset' the proxy.
System.clearProperty("http.proxyHost");

// From now on HTTP connections will be done directly.

这篇关于使用Java中的代理代码连接到网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆