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

发布于 2024-09-05 04:20:09 字数 1520 浏览 3 评论 0原文

我想通过java中的代理连接到as站点。这是我编写的代码:

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

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

java.lang.IllegalArgumentException:消息标头值中存在非法字符:Basic 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)

知道怎么做吗?

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: 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)

Any Idea how to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我的奇迹 2024-09-12 04:20:09

如果您只是尝试通过 HTTP 代理服务器发出 HTTP 请求,则不需要付出如此多的努力。这里有一篇文章:http://java.sun。 com/javase/6/docs/technotes/guides/net/proxies.html

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

// 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.

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

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.
花桑 2024-09-12 04:20:09

在我看来,您根本没有使用 Proxy 实例。我认为您应该在创建 URLConnection 实例时传递它:

URLConnection connection=url.openConnection(proxy);

设置环境属性 http.proxy 更容易,并且在使用某些没有代理实例的第三方库时只能传递支持解决方案,但它的缺点是它是为整个过程全局设置的。

It seems to me, that you are not using your Proxy instance at all. I think you should pass it when you are creating URLConnection instance:

URLConnection connection=url.openConnection(proxy);

Setting of environment properties http.proxy is easier and when using some 3rd party libraries without Proxy instance passing support only possible solution, but its drawback is that it is set globally for the whole process.

拥醉 2024-09-12 04:20:09

我正在使用 Google 数据 API,让代理设置起作用的唯一方法是提供与代理相关的所有参数,即使它们设置为空:

/usr/java/jdk1.7.0_04/bin/java -Dhttp.proxyHost=10.128.128.13 
    -Dhttp.proxyPassword -Dhttp.proxyPort=80 -Dhttp.proxyUserName 
    -Dhttps.proxyHost=10.128.128.13 -Dhttps.proxyPassword -Dhttps.proxyPort=80 
    -Dhttps.proxyUserName com.stackoverflow.Runner

其中,不需要用户名和密码,并且相同的 http和 https 服务器设置为相同,以及端口号(如果也是您的情况)。请注意,还提供了相同的 HTTP 代理作为 HTTPS 服务器及其端口号(参考自 https://code.google.com/p/syncnotes2google/issues/detail?id=2#c16)。

如果您的 Java 类有“URL”类的实例,它应该选择这些配置...

I was using the Google Data APIs and the only way I got the proxy settings to work was to provide ALL the parameters related to proxy, even thought they are set to be empty:

/usr/java/jdk1.7.0_04/bin/java -Dhttp.proxyHost=10.128.128.13 
    -Dhttp.proxyPassword -Dhttp.proxyPort=80 -Dhttp.proxyUserName 
    -Dhttps.proxyHost=10.128.128.13 -Dhttps.proxyPassword -Dhttps.proxyPort=80 
    -Dhttps.proxyUserName com.stackoverflow.Runner

Where, username and password are NOT required, and the same http and https servers are set to be the same, as well as the port number (if that's your case as well). Note that the same HTTP proxy is also provided as the HTTPS server, as well as its port number (reference from https://code.google.com/p/syncnotes2google/issues/detail?id=2#c16).

If your Java class has an instance of the class "URL", it should pick those configurations up...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文