通过代理java创建套接字

时间:2012-09-24 10:30:50

标签: java http sockets proxy

我已经编写了代码,必须通过套接字使用http HEAD请求找到文件大小...我在我的家中尝试使用非代理连接它的工作...但是当我在我的大学里用代理尝试它服务器的IP是172.16.4.6,端口1117不工作......任何建议......谢谢。

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));]

2 个答案:

答案 0 :(得分:1)

你为自己做的事情很难。使用HttpURLConnection通过代理连接:

HttpConnection conn = (HttpConnection) url_of_file.openConnection(proxy);
conn.setRequestMethod("HEAD");
Totalsize = conn.getContentLength();

还有其他HTTP客户端可以做得更好,但除非现有客户端不能满足您的需求,否则不应创建自己的客户端!

答案 1 :(得分:1)

感谢评论中的线索。我找到了问题的解决方案。我使用错误的端口构建InetSocketAddress两次的代码中存在一个错误。完整的工作代码如下。

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);
    }