crear socket a través de proxy java

He escrito el código que tiene que encontrar el tamaño del archivo usando la solicitud http HEAD a través de sockets ... lo intento en mi casa con una conexión no proxy que funciona ... pero cuando lo intento en mi universidad con un servidor proxy cuya ip es 172.16.4.6 y el puerto 1117 no funciona ... alguna sugerencia ... gracias.

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

El error que obtengo es:

Unknown host exception at for code [ mysocket2.connect(
    new InetSocketAddress(hostaddress,80));]
Respuesta 1

Estás haciendo las cosas muy difíciles para ti. Use HttpURLConnection para conectarse a través del proxy:

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

Hay otros clientes HTTP que hacen un mejor trabajo, ¡pero no debe crear uno propio a menos que los clientes existentes no hagan lo que necesita!

Respuesta: 2

With scala I have some data, for ex. val values = Seq(0, 2, 10, 50) And I defined buckets, eg. val buckets = TreeMap[Int, Double]((0, -0.001), (1, 1.5), ...

I have project with camel parent. <parent> <groupId>org.apache.camel</groupId> <artifactId>camel-parent</artifactId> <version>2.23.1</...

I've used chkBox.setIcon(); chkBox.setSelectedIcon(); chkBox.setDisabledIcon(); chkBox.setDisabledSelectedIcon(); to set custom icons for my JCheckbox. But now, if the focus moves to one of the ...

I want to create an index for an specific sql call in which I join two tables and I don't know how to insert the outer column. This is what I currently have: <changeSet id="1234" author="name" &...