2012-03-15 3 views
0

私は動作しているHTTPプロキシサーバーを持っています。 HTTPだけを使用してウェブサイトを適切に処理でき、HTTPSウェブサイトにも接続することができます。クライアントのCONNECT要求と適切に交渉し、宛先サーバーへの接続を開始できます。私の問題は、facebookやyahooのようなユーザーのログインが必要なHTTPS Webサイトの一部です(ただし、一部のサイトでは正常に実行できます)。 (クライアントとサーバー)クライアントからのメッセージをHTTPSでWeb Serverに正しく転送する方法はありますか。 (Java Proxy Server)

上記のサイトにログインしようとするたびに何が起こるのですか。ブラウザはアカウントに正常にログインしたが、実際のページを表示する代わりにログインページに戻ります。ここ

は私と信じてどのように私のコードの作業

//デーモンクラス

while(true) 
{ 
    ConnectionHandler handler = new ConnectionHandler(server.accept()); 
    handler.start(); 
} 

// ConnectionHandlerクラス

class ConnectionHandler extends Thread 
{ 
    Socket client = null; 
    Socket server = null; 
    HTTPRequestHdr request = null; 
    public ConnectionHandler(Socket socket) 
    { 
     client = socket; 
     request = new HTTPRequestHdr(); 
    } 

    public void run() 
    { 
     try 
     { 
     request.parse(client.getInputStream()); 
     server = new Socket(request.url,request.port); 
     if(request.getMethod().equals("CONNECT"); 
     { 
      // send 200 message to client then proceed 
     } 
     else 
     { 
      // send the request to server 
     } 

     Tunnel clientToServer = new Tunnel(client,server); 
     Tunnel ServerToClient = new Tunnel(server,client); 
     clientToServer.start(); 
     ServerToClient.start(); 

     }catch(Exception e) {} 
    } 
} 

//トンネルクラス

class Tunnel extends Thread 
{ 
    Socket from = null; 
    Socket to = null; 
    public Tunnel(Socket from , Socket to) 
    { 
     this.from = from; 
     this.to = to; 
    } 

    public void run() 
    { 
     OutputStream toClient = null; 
     InputStream fromClient = null; 
     byte[] buffer = new byte[50]; 
     int numberRead = 0; 
     iny noResponse = 0; 
     try 
     { 
     toClient = to.getOutputStream(); 
     fromClient = fro.getInputStream(); 

     int avail = fromClient.available(); 

     if(avail < 1) 
     { 
      Thread.sleep(20); 
      noResponse += 20; 
     } 
     else 
     { 
      noResponse = 0; 
     } 

     while(avail > 0) 
     { 
      numberRead = avail; 
      numberRead = fromClient.read(buffer,0,numberRead); 

      toClient.write(buffer,0,numberRead); 
      toClient.flush(); 
      avail = fromClient.available(); 
     } 

     if(noResponse > 30000) 
     { 
      // close connection 
     } 

     }catch(Exception e) {} 

    } 
} 

ですプロに失敗する両方の側から来るデータを転送する。 私は間違っていると私はそれを修正する方法を教えてください?

答えて

0
try 
{ 
    int avail = fromClient.available(); 

    if(avail < 1) 
    { 
     Thread.sleep(20); 
     noResponse += 20; 
    } 
    else 
    { 
     noResponse = 0; 
    } 
    while(avail > 0) 
    { 
     numberRead = avail; 
     numberRead = fromClient.read(buffer,0,numberRead); 
     toClient.write(buffer,0,numberRead); 
     toClient.flush(); 
     avail = fromClient.available(); 
    } 
    if(noResponse > 30000) 
    { 
     // close connection 
    } 
} catch(Exception e) {} 

これはすべて間違っています。ソケットにはすでにタイムアウト機能があり、時間を無駄にしているだけでavailable()sleep()を呼び出すだけです。必要なのは次のとおりです。

try 
{ 
    fro.setSoTimeout(30000); 
    while ((length = fromClient.read(buffer)) > 0) 
    { 
     toClient.write(buffer, 0, length); 
    } 
    // close sockets 
    toClient.close(); 
    fromClient.close(); 
} 
catch (SocketTimeoutException exc) 
{ 
    // handle timeout, and certainly close the socket 
} 
catch (IOException exc) 
{ 
    // exception handling 
} 
finally 
{ 
    fro.close(); // close the input socket, NB not the output socket, the other thread will do that 
} 
関連する問題