java.net.UnknownHostException:主机是未解析的 SOCKS 代理
Posted
技术标签:
【中文标题】java.net.UnknownHostException:主机是未解析的 SOCKS 代理【英文标题】:java.net.UnknownHostException: Host is unresolved SOCKS proxy 【发布时间】:2018-11-13 03:06:55 【问题描述】:我在特定端口上设置了 Tor,并希望通过该端口访问该网站。以下代码在 android 7.0 中完美运行,但在 android 6.0 中出现以下错误。可能是什么问题? Tor 服务在我发送请求的特定端口成功运行。 运行这个库的示例代码(https://github.com/jehy/Tor-Onion-Proxy-Library)
错误
java.net.UnknownHostException:主机未解析:google.com W/System.err:在 java.net.Socket.connect(Socket.java:893) W / System.err:在 cz.msebera.android.httpclient.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:74) W / System.err:在 com.example.nandan.sampletorproxyapp.MyConnectionSocketFactory.connectSocket(MyConnectionSocketFactory.java:33) W / System.err:在 cz.msebera.android.httpclient.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:134) W / System.err:在 cz.msebera.android.httpclient.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353) W / System.err:在 cz.msebera.android.httpclient.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380) W / System.err:在 cz.msebera.android.httpclient.impl.execchain.MainClientExec.execute(MainClientExec.java:236) W / System.err:在 cz.msebera.android.httpclient.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) W / System.err:在 cz.msebera.android.httpclient.impl.execchain.RetryExec.execute(RetryExec.java:88) W / System.err:在 cz.msebera.android.httpclient.impl.execchain.RedirectExec.execute(RedirectExec.java:110) W / System.err:在 cz.msebera.android.httpclient.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) W / System.err:在 cz.msebera.android.httpclient.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) W / System.err:在 cz.msebera.android.httpclient.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55) W / System.err:在 com.example.nandan.sampletorproxyapp.MainActivity$TorTask.doInBackground(MainActivity.java:85) W / System.err:在 com.example.nandan.sampletorproxyapp.MainActivity$TorTask.doInBackground(MainActivity.java:60) W/System.err:在 android.os.AsyncTask$2.call(AsyncTask.java:295) W / System.err:在 java.util.concurrent.FutureTask.run(FutureTask.java:237) W/System.err: 在 android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) W / System.err:在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) W / System.err:在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) W/System.err:在 java.lang.Thread.run(Thread.java:818)
主要活动
HttpClient httpClient = getNewHttpClient();
int port = onionProxyManager.getIPv4LocalHostSocksPort();
InetSocketAddress socksaddr = new InetSocketAddress("127.0.0.1", port);
HttpClientContext context = HttpClientContext.create();
context.setAttribute("socks.address", socksaddr);
HttpGet httpGet = new HttpGet("http://google.co.in");
HttpResponse httpResponse = httpClient.execute(httpGet, context); // GETTING ERROR HERE
static class FakeDnsResolver implements DnsResolver
@Override
public InetAddress[] resolve(String host) throws UnknownHostException
return new InetAddress[] InetAddress.getByAddress(new byte[] 1, 1, 1, 1 ) ;
public HttpClient getNewHttpClient()
Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", new MyConnectionSocketFactory())
.build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(reg,new FakeDnsResolver());
return HttpClients.custom()
.setConnectionManager(cm)
.build();
MyConnectionSocketFactory.java
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import cz.msebera.android.httpclient.HttpHost;
import cz.msebera.android.httpclient.conn.socket.PlainConnectionSocketFactory;
import cz.msebera.android.httpclient.protocol.HttpContext;
public class MyConnectionSocketFactory extends PlainConnectionSocketFactory
@Override
public Socket createSocket(final HttpContext context)
InetSocketAddress socksaddr = (InetSocketAddress) context.getAttribute("socks.address");
Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr);
return new Socket(proxy);
@Override
public Socket connectSocket(
int connectTimeout,
Socket socket,
final HttpHost host,
final InetSocketAddress remoteAddress,
final InetSocketAddress localAddress,
final HttpContext context) throws IOException
InetSocketAddress unresolvedRemote = InetSocketAddress
.createUnresolved(host.getHostName(), remoteAddress.getPort());
return super.connectSocket(connectTimeout, socket, host, unresolvedRemote, localAddress, context);
【问题讨论】:
【参考方案1】:尝试改变
HttpGet httpGet = new HttpGet("http:google.co.in");
到
HttpGet httpGet = new HttpGet("http://google.co.in");
或
URI uri = new URIBuilder()
.setScheme("http")
.setHost("google.co.in")
.build();
HttpGet httpGet = new HttpGet(uri);
更新 - 试试这个
HttpHost target = new HttpHost("google.co.in", 80, "http");
HttpGet httpGet = new HttpGet("/");
HttpResponse httpResponse = httpClient.execute(target, httpGet, context);
如果它不起作用,我不太确定。也许试试这个链接希望这有助于How to use HttpClientBuilder with Http proxy?
【讨论】:
我使用的是HttpGet httpGet = new HttpGet("http://google.co.in");
,对不起,我确实弄错了为什么发布这个问题。第二个也不行。
更新了我的答案。希望对您有所帮助
另外,如果您打印port
变量,它会给您一个有效的端口号还是为空?以上是关于java.net.UnknownHostException:主机是未解析的 SOCKS 代理的主要内容,如果未能解决你的问题,请参考以下文章