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

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

问题描述

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

公共类 ConnectThroughProxy{代理 proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy ip", 8080));公共静态无效主要(字符串 [] 参数){尝试{URL url = 新 URL("http://www.rgagnon.com/javadetails/java-0085.html");URLConnection 连接=url.openConnection();字符串编码 = new String(Base64.encode(new String("user_name:pass_word").getBytes()));连接.setDoOutput(true);connection.setRequestProperty("代理授权","基本"+encoded);字符串页面="";字符串线;StringBuffer tmp = new StringBuffer();BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));而 ((line=in.readLine()) != null){page.concat(line + "
");}System.out.println(page);}捕捉(异常前){ex.printStackTrace();}}

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

<块引用>

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)

知道怎么做吗?

解决方案

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

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

<上一页>//将 http 代理设置为 webcache.mydomain.com:8080System.setProperty("http.proxyHost", "webcache.mydomain.com");System.setProperty("http.proxyPort", "8080");//下一个连接将通过代理.URL url = 新 URL("http://java.sun.com/");InputStream in = url.openStream();//现在,让我们取消设置"代理.System.clearProperty("http.proxyHost");//从现在开始,HTTP 连接将直接完成.

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 + "
");
            }
            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?

解决方案

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.

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

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