我正在尝试构建代理服务器,最近我正在研究 https。如本文所述。我试图通过隧道连接请求。我的代码如下:
private boolean handleConnect(HttpServletRequest req,HttpServletResponse response){
String uri=req.getRequestURI();
String port="";
String host="";
int c=uri.indexOf(":");
if (c >= 0){
port = uri.substring(c + 1);
host = uri.substring(0,c);
if (host.indexOf('/') > 0)
host = host.substring(host.indexOf('/') + 1);
}
// Make Asyncronous connection
try{
InetSocketAddress inetAddress = new InetSocketAddress(host,Integer.parseInt(port));
{
InputStream in=req.getInputStream();
OutputStream out=response.getOutputStream();
if(true){
Socket sock=new Socket(host,Integer.parseInt(port));
IO.copy(in, sock.getOutputStream());
IO.copy(sock.getInputStream(), out);
if(!sock.getKeepAlive()){
sock.close();
}
}
}
}
catch(Exception ex){
ex.printStackTrace();
return false;
}
return true;
}
的代码结果java.net.UnknownHostException: google.com.np
和https://google.com.np
超时https://Facebook.com
。这是为什么 ??请建议隧道连接 HTTP 请求的最佳方式。