有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在HTTPS中正确地将来自客户端的消息转发到Web服务器?(Java代理服务器)

我有一个工作的HTTP代理服务器。它可以只使用HTTP正确处理网站,也可以连接到HTTPS网站。它可以正确地与客户端的连接请求协商,并开始连接到目标服务器。现在我的问题是一些需要用户登录的HTTPS网站,比如facebook和yahoo(虽然有些网站我可以成功登录)。似乎我无法正确转发来自双方的消息。(客户端和服务器)

每次我尝试登录上述网站时都会发生这种情况。浏览器显示它已成功登录我的帐户,但它不会显示实际页面,而是返回到登录页面

下面是我的代码是如何工作的

//守护进程类

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

   }
}

我认为我未能正确转发双方的数据。请告诉我在哪里 我错了,怎么解决


共 (1) 个答案

  1. # 1 楼答案

    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
    }