2012-09-24 68 views
1

我已經寫了代碼,它必須通過套接字使用http HEAD請求來查找文件大小......我在我的家中嘗試使用非代理連接工作......但是當我嘗試在我的大學有一臺代理服務器,其IP爲172.16.4.6,端口1117不工作......任何建議......謝謝。通過代理創建套接字java

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

你是什麼意思與「不工作」?你是否收到某種錯誤信息?例外?還是隻是沒有? – Philipp

+0

它給代碼[mysocket2.connect(new InetSocketAddress(hostaddress,80));] – user1547163

回答

2

感謝您的評論中的線索。我找到了解決問題的辦法。代碼中有一個錯誤,我使用錯誤的端口構造了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); 
    } 
+0

在我的情況下,當我第一次加載頁面時,SOCKS服務器接受但是沒有連接.. 我從控制檯看過.. 另外它也是沒有連接,當我打開我的webview中的另一個網址.. 你可以在這方面指導我? – Noman

1

你是爲自己做的事情很辛苦。使用HttpURLConnection通過代理連接:

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

還有其他的HTTP客戶端裏面做一個更好的工作,但除非現有的客戶不這樣做,你需要什麼,你不應該創建自己的!

+0

實際上我想創建一個到遠程服務器的套接字連接。並使用插座我想找到文件大小..... – user1547163

+0

這就是我的代碼也是。事實上,除了免費的代理支持外,它和你的幾乎一樣。 ;) –

+0

haammm !!是的大衛格蘭特先生你是非常正確的,但我正在尋找代理服務器套接字連接的基本編碼級別。它的貨物對於初學者來說有基本概念..... – user1547163