1

I have written the code which have to find the file size using http HEAD request through sockets... i try it in my home with non proxy connection its working ... but when i try it in my college with a proxy server whose ip is 172.16.4.6 and port 1117 it is not working......any suggestion......thanks.

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

The error I get is:

Unknown host exception at for code [ mysocket2.connect(
    new InetSocketAddress(hostaddress,80));]
2
  • What do you mean with "not working"? Do you get some kind of error message? An exception? Or does it just nothing? Commented Sep 24, 2012 at 12:27
  • it gives unknown host exception at for code [ mysocket2.connect(new InetSocketAddress(hostaddress,80));] Commented Sep 24, 2012 at 14:09

2 Answers 2

1

You are making things very hard for yourself. Use HttpURLConnection to connect through the proxy:

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

There are other HTTP clients which do a better job, but you should not create your own unless existing clients don't do what you need!

Sign up to request clarification or add additional context in comments.

3 Comments

actually i want to create a socket connection to the remote server. and using socket i want to find the file size.....
That's what my code does too. In fact, it does pretty much the same as yours, except you get the proxy support for free. ;)
haammm!! yes mr David Grant you are quite right but i was looking for the basic level of coding for proxy server socket connection. and its goods for beginner to have the basic concepts.....
1

Thanks for the clues in comments. I found the solution to the problem. There was a bug in the code I was constructing InetSocketAddress twice using a wrong port. The complete working code is below.

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

1 Comment

In my case when i load the page for the first time, SOCKS server accept but does not connect.. i have seen from console.. Also it does not connects when i open another url in my webview.. Can you plz guide me in this regard ??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.