几天来,我一直致力于在 vb.net 中创建透明代理应用程序。到目前为止一切都很好。我很难找到将数据从客户端镜像到服务器的最佳方式,反之亦然。
我尝试了多种不同的方法。这是我最新的。到目前为止它工作得最好,但页面永远不会完成加载。
Do Until NeedToClose = True
If client.Connected = True Then
If client.GetStream.DataAvailable = True Then
Dim buffer(client.ReceiveBufferSize) As Byte
Dim length As Integer
Try
length = client.GetStream.Read(buffer, 0, buffer.Length) 'fills buffer with request
server.GetStream.Write(buffer, 0, length)
Catch ex As Exception
End Try
End If
If server.Connected = False Then
'client is holding connection open. server has already disconnected
stopWorker()
End If
If server.Connected = True Then
If server.GetStream.DataAvailable = True Then
Dim buffer(server.ReceiveBufferSize) As Byte
Dim length As Integer
Try
length = server.GetStream.Read(buffer, 0, buffer.Length) 'fills buffer with request
client.GetStream.Write(buffer, 0, length)
Catch ex As Exception
End Try
End If
If client.Connected = False Then
'server is holding connection open. client has already disconnected
stopWorker()
End If
End If
End If
If client.Connected = False And server.Connected = False Then
stopWorker()
End If
Loop
可能不是实现这一目标的最佳方式。Try..Catch 只是用于设置断点。我已经收到来自客户端的连接,拉出 Host 属性,并连接到服务器。然后,我将从客户端收到的第一个请求发送到服务器。然后,您在上面看到的代码启动并开始转发请求和回复。
有一个更好的方法吗?为什么页面没有完全加载? 谢谢!