我使用Jodd-Http库来连接代理:
ProxyInfo proxyInfoObj = new ProxyInfo(ProxyType.HTTP, "10.30.56.70", 8080, "", "");
SocketHttpConnectionProvider provider = new SocketHttpConnectionProvider();
provider.useProxy(proxyInfoObj);
HttpRequest request = HttpRequest.get(url);
request.method("GET");
request.charset("UTF-8");
HttpResponse response = request.open(provider).send();
result = response.bodyText();
但我得到了这个错误:
jodd.http.HttpException: HTTP: Invalid code
at jodd.http.net.HTTPProxySocketFactory.createHttpProxySocket(HTTPProxySocketFactory.java:113)
at jodd.http.net.HTTPProxySocketFactory.createSocket(HTTPProxySocketFactory.java:32)
如果我使用SOCKS4类型,程序将挂起并且不返回任何内容。有人能帮我吗?
但我可以使用以下代码通过代理连接:
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.30.56.70", 8080));
HttpURLConnection connection =(HttpURLConnection)new URL("http://tvl.csmtalk.vn/api/sms/receive").openConnection(proxy);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-type", "text/xml");
connection.setRequestProperty("Accept", "text/xml, application/xml");
connection.setRequestMethod("GET");
connection.connect();
对我来说,这两个代码都挂起了。当我尝试Jodd时,它挂起了,因为它无法打开到10.30.56.70:8080
的代理套接字。当我尝试时
telnet 10.30.56.70 8080
在命令行中,它也挂起。代理似乎没有响应。(如果您需要更多详细信息,或者如果您想发送一些有关连接的私人数据,您可以联系Jodd支持。)
顺便说一句,你不需要:
request.method("GET");
request.charset("UTF-8");
因为方法get()
已经将方法设置为GET
,并且字符集不用于请求,而是用于响应(如果服务器未设置,则设置一个)。