Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在HTTPS中正确地将来自客户端的消息转发到Web服务器?(Java代理服务器)_Java_Http_Proxy_Forward - Fatal编程技术网

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

如何在HTTPS中正确地将来自客户端的消息转发到Web服务器?(Java代理服务器),java,http,proxy,forward,Java,Http,Proxy,Forward,我有一个工作的HTTP代理服务器。它可以只使用HTTP正确处理网站,也可以连接到HTTPS网站。它可以正确地与客户端的连接请求协商,并开始连接到目标服务器。现在我的问题是一些需要用户登录的HTTPS网站,比如facebook和yahoo(虽然有些网站我可以成功登录)。似乎我无法正确转发来自双方的消息。(客户端和服务器) 每次我尝试登录到上述站点时都会发生这种情况。浏览器显示它已成功登录我的帐户,但它不会显示实际页面,而是返回到登录页面 下面是我的代码是如何工作的 //守护进程类 while(tr

我有一个工作的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) {}

   }
}
类隧道扩展线程
{
socketfrom=null;
套接字到=空;
公共隧道(插座从,插座到)
{
this.from=from;
这个;
}
公开募捐
{
OutputStream-toClient=null;
InputStream fromClient=null;
字节[]缓冲区=新字节[50];
int numberRead=0;
iny-noResponse=0;
尝试
{
toClient=to.getOutputStream();
fromClient=fro.getInputStream();
int avail=fromClient.available();
如果(有效值<1)
{
睡眠(20);
noResponse+=20;
}
其他的
{
noResponse=0;
}
而(效用>0)
{
numberRead=有效;
numberRead=fromClient.read(缓冲区,0,numberRead);
写入(缓冲区,0,numberRead);
toClient.flush();
avail=fromClient.available();
}
如果(无响应>30000)
{
//密切联系
}
}捕获(例外e){}
}
}
我认为我未能正确地转发来自双方的数据。请告诉我在哪里 我错了,怎么纠正呢

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