Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8590343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:16:24+00:00 2026-06-11T23:16:24+00:00

I have written the code which have to find the file size using http

  • 0

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));]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:16:25+00:00Added an answer on June 11, 2026 at 11:16 pm

    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);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a code in which i want to open a HTM file
I have written PHP code which connects to MYSQL to look up information, sends
I have written a code which does the thing as the title says. The
Need assistance with the pexpect module I have written a simple code which would
I have written two pieces of code which I intended to have identical outputs,
I have written some code in JSP to download .docx files which is hosted
I have written to code to read and write to registry which is working
I have written a custom server control which (pseudo-code) looks like public class MyCustomCtrl
I have code written in c, which contains some complex algorithms and I want
Let's assume I have a framework written in Java and some C++ code which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.