通过代理java创建套接字
我已经写的代码,必须找到通过套接字使用http头请求的文件大小。。。我试着在我家用非代理连接它的工作。。。但是,当我在我的大学里用一个ip为172.16.4.6,端口为1117的代理服务器尝试它时,它不起作用……任何建议……谢谢通过代理java创建套接字,java,http,sockets,proxy,Java,Http,Sockets,Proxy,我已经写的代码,必须找到通过套接字使用http头请求的文件大小。。。我试着在我家用非代理连接它的工作。。。但是,当我在我的大学里用一个ip为172.16.4.6,端口为1117的代理服务器尝试它时,它不起作用……任何建议……谢谢 public class Proxytesting { private static OutputStream os; private static InputStream in; private static BufferedReader r
public class Proxytesting {
private static OutputStream os;
private static InputStream in;
private static BufferedReader reader;
private static int Totalsize;
public static void main(String[] args) {
try {
URL url_of_file=new URL("http://www.stockvault.net/data/s/124348.jpg");
String hostaddress=url_of_file.getHost();
////////////////////for proxy server ///////////////////
String textip="172.16.4.7";
InetAddress host=InetAddress.getByName(textip);
System.out.println(host);
int port=1117;
SocketAddress ad=new InetSocketAddress(host, 1117);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, ad);
Socket mysocket2 = new java.net.Socket();
mysocket2.connect(new InetSocketAddress(hostaddress,80));
System.out.println("Socket opened to " + hostaddress + "\n");
String file=url_of_file.getFile();
System.out.println(" file = "+file);
os = mysocket2.getOutputStream();
String headRequest = "HEAD " +url_of_file+" HTTP/1.1\r\n"
+ "Host: "+ hostaddress +"\r\n\r\n";
os.write(headRequest.getBytes());
in = mysocket2.getInputStream();
reader= new BufferedReader(new InputStreamReader(in));
String contlength="Content-Length:";
// 1. Read the response header from server separately beforehand.
String response;
Totalsize = 0;
do{
response = reader.readLine();
if(response.indexOf("Content-Length") > -1)
{
Totalsize = Integer.parseInt(response.substring(response.indexOf(' ')+1));
response = null;
}
}while(response != null);
System.out.println(" cont_lentht ##### == "+Totalsize);
} catch (IOException ex) {
Logger.getLogger(Proxytesting.class.getName()).log(Level.SEVERE, null, ex);
}
我得到的错误是:
Unknown host exception at for code [ mysocket2.connect(
new InetSocketAddress(hostaddress,80));]
你让事情变得很艰难。用于通过代理进行连接:
HttpConnection conn = (HttpConnection) url_of_file.openConnection(proxy);
conn.setRequestMethod("HEAD");
Totalsize = conn.getContentLength();
还有其他HTTP客户机做得更好,但是您不应该创建自己的HTTP客户机,除非现有客户机不能满足您的需要 感谢评论中的线索。我找到了解决问题的办法。我使用错误的端口两次构造
InetSocketAddress
的代码中有一个bug。完整的工作代码如下
public class Proxytesting {
private static OutputStream os;
private static InputStream in;
private static BufferedReader reader;
private static int Totalsize;
public static void main(String[] args) {
try {
URL url_of_file=new URL("http://www.stockvault.net/data/s/124348.jpg");
String hostaddress=url_of_file.getHost();
////////////////////for proxy server ///////////////////
String textip="172.16.4.7";
InetAddress host=InetAddress.getByName(textip);
System.out.println(host);
int port=1117;
SocketAddress ad=new InetSocketAddress(host, 1117);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, ad);
Socket mysocket2 = new java.net.Socket();
mysocket2.connect(ad);
System.out.println("Socket opened to " + hostaddress + "\n");
String file=url_of_file.getFile();
System.out.println(" file = "+file);
os = mysocket2.getOutputStream();
String headRequest = "HEAD " +url_of_file+" HTTP/1.1\r\n"
+ "Host: "+ hostaddress +"\r\n\r\n";
os.write(headRequest.getBytes());
in = mysocket2.getInputStream();
reader= new BufferedReader(new InputStreamReader(in));
String contlength="Content-Length:";
// 1. Read the response header from server separately beforehand.
String response;
Totalsize = 0;
do{
response = reader.readLine();
if(response.indexOf("Content-Length") > -1)
{
Totalsize = Integer.parseInt(response.substring(response.indexOf(' ')+1));
response = null;
}
}while(response != null);
System.out.println(" cont_lentht ##### == "+Totalsize);
} catch (IOException ex) {
Logger.getLogger(Proxytesting.class.getName()).log(Level.SEVERE, null, ex);
}
“不工作”是什么意思?你收到什么错误信息了吗?例外?还是什么都没有?它在代码[mysocket2.connect(新的InetSocketAddress(hostaddress,80));]处给出未知主机异常,实际上我想创建到远程服务器的套接字连接。使用socket我想找到文件大小……这也是我的代码所做的。事实上,它的功能与您的基本相同,只是您可以免费获得代理支持。;)哈姆!!是的,David Grant先生,你说得很对,但我一直在寻找代理服务器套接字连接的基本编码。对于初学者来说,它有一些基本的概念…在我的情况下,当我第一次加载页面时,SOCKS服务器接受但不连接。。我从控制台看到了。。当我在我的webview中打开另一个url时,它也不会连接。。你能在这方面指导我吗??