本文介绍了为什么我的Http侦听器无法捕获来自Web服务器的所有响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代理服务器,该代理服务器将来自客户端的请求发送到Web服务器,并接收来自Web服务器的响应并将其发送到客户端.几乎是.我遇到过这样的情况,客户端请求一个网页,服务器发送 返回状态码为302的响应以及状态码为200的另一个响应.我似乎没有捕获302页面,这是我真正需要的页面,因为其中包含身份验证cookie!我知道网络服务器正在发送 它回来了,因为我在代理代码所在的机器上使用了wireshark.

I have a proxy server that sends the requests from the client to the webserver and receives the responses from the webserver and sends it to the client.  ALMOST.  I've come across a situation where the client asks for a webpage, the server sends back a response with a status code of 302 as well as another response with a status code 200.  I don't seem to capture the 302 page, and that's the one that I really need because it has an authentication cookie in it!  I know the webserver is sending it back because I use wireshark on the machine where the proxy code is.

精简但仍很冗长的代码如下所示.任何帮助将不胜感激.底线-我没有在请求的端口上获得所有流量(80);我知道我不是出于某种原因而被困.哦,我可以补充 在wireshark输出中,如果有人需要的话.

The stripped down, but still lengthy code, is shown below.  Any help would REALLY be appreciated.  Bottom line - I'm not getting all the traffic on the port I've requested (80); I know I'm not trapping for some reason.  Oh, and I could add in the wireshark output if anyone needs it.

谢谢提前
戴夫

Thanks  in advance,
Dave

长但仍统一的代码:

private void Startup(){

private void Startup(){

    _listener = new HttpListener();

     _listener = new HttpListener();

    _listener.Prefixes.Add("http://*:80/;");

     _listener.Prefixes.Add("http://*:80/;");

    _listener.Start();

     _listener.Start();

    /* 创建一个非阻塞的异步Begin Context侦听器,该侦听器将调用
     *  http请求到达时,http请求开始上下文处理程序*/

     /*  Create a non blocking async Begin Context Listener which will call
      *  the http request begin context handler when an http request arrives */

    IAsyncResult结果= _listener.BeginGetContext(
      新的AsyncCallback(_HttpRequestBeginContextEventHandler),_ listener);

     IAsyncResult result =_listener.BeginGetContext(
        new AsyncCallback(_HttpRequestBeginContextEventHandler), _listener);

}

      

       

private void _HttpRequestBeginContextEventHandler(IAsyncResult result){

private void _HttpRequestBeginContextEventHandler(IAsyncResult result) {

      如果(_listener == null)返回;

        if (_listener == null) return;

       /*找出上下文对象*/

        /* Get out the context object   */

       HttpListenerContext context =

        HttpListenerContext context =

           _listener.EndGetContext(result);

            _listener.EndGetContext(result);

       /* 立即设置下一个开始上下文*/

        /*  Immediately set up the next begin context */

       _listener.BeginGetContext(

        _listener.BeginGetContext(

          新的AsyncCallback(_HttpRequestBeginContextEventHandler),_ listener);

            new AsyncCallback(_HttpRequestBeginContextEventHandler), _listener);

       /* 将流程请​​求放在其自己的工作线程上,这样就不会

        /*  Put the Process Request on its own worker thread, so it does not

           * 捆扎其他东西*/

            *  tie up other stuff  */

       ThreadPool.QueueUserWorkItem(

        ThreadPool.QueueUserWorkItem(

          新的System.Threading.WaitCallback(_ProcessRequest),上下文);

            new System.Threading.WaitCallback(_ProcessRequest), context);

   }

    }

private void _ProcessRequest(对象参数){

private void _ProcessRequest(object parameter) {

       /* 将参数转换为http监听器上下文*/

        /*  Convert parameter to http listener context  */

       HttpListenerContext context =(HttpListenerContext)parameter;

        HttpListenerContext context = (HttpListenerContext)parameter;

       /* 创建一个实例来保存Web请求数据*/

        /*  Create an instance to hold the web request data */

       ClientWebRequest clientWebRequest = new ClientWebRequest();

        ClientWebRequest clientWebRequest = new ClientWebRequest();

       /* 创建一个ID来标识进程*/

        /*  Create a ID to identifiy the process */

       Guid currentProcessID = Guid.NewGuid();

        Guid currentProcessID = Guid.NewGuid();

       clientWebRequest.processID = currentProcessID;

        clientWebRequest.processID = currentProcessID;

       clientWebRequest.clientRequest = context.Request;

        clientWebRequest.clientRequest = context.Request;

       clientWebRequest.clientResponse = context.Response;          

        clientWebRequest.clientResponse = context.Response;           

       /* 向远程Web服务器发出请求*/

        /*  Make a request to the remote web server */

       _ProcessRemoteWebServerRequest(ref clientWebRequest);

        _ProcessRemoteWebServerRequest(ref clientWebRequest);

      如果(clientWebRequest.remoteWebServerRequest!= null&

        if (clientWebRequest.remoteWebServerRequest != null &&

           clientWebRequest.remoteWebServerResponse!= null)

            clientWebRequest.remoteWebServerResponse != null)

/* 将远程Web服务器的响应发送到客户端的浏览器*/

                   /*  Send the remote web server's response to the client's browser   */

                          _ProcessRemoteWebServerResponse(ref clientWebRequest);

                  _ProcessRemoteWebServerResponse(ref clientWebRequest);

}

private void _ProcessRemoteWebServerRequest(ref ClientWebRequest clientWebRequest){

private void _ProcessRemoteWebServerRequest(ref ClientWebRequest clientWebRequest) {              

       /* 获取嵌入在客户请求中的查询字符串*/

        /*  Get the query string embedded in the client's request   */

       /* 打开到客户端请求的数据流*/

        /*  Open a data stream to the client request    */

       StreamReader getPostParameters =

        StreamReader getPostParameters =

          新的StreamReader(clientWebRequest.clientRequest.InputStream,true);

            new StreamReader(clientWebRequest.clientRequest.InputStream, true);

       /* 阅读客户请求的查询字符串*/

        /*  Read the client request's query string  */

       clientQueryString = getPostParameters.ReadToEnd();

        clientQueryString = getPostParameters.ReadToEnd();

       /* 关闭数据流*/

        /*  Close the data stream */

       getPostParameters.Close();

        getPostParameters.Close();

       /* 设置到远程Web服务器的连接字符串*/

        /*  Setup the connection string to the remote web server    */

      字符串remoteWebServerConnectionString =

        string remoteWebServerConnectionString =

           @"http://aaa.bbb.ccc.ddd" + clientWebRequest.clientRequest.RawUrl;

            @"http://aaa.bbb.ccc.ddd" + clientWebRequest.clientRequest.RawUrl;

       /* 创建到远程Web服务器的实例请求*/              

        /*  Create an instanced request to the remote web server */                

       clientWebRequest.remoteWebServerRequest =

        clientWebRequest.remoteWebServerRequest =

           (HttpWebRequest)WebRequest.Create(remoteWebServerConnectionString);

            (HttpWebRequest)WebRequest.Create(remoteWebServerConnectionString);

      如果(clientWebRequest.clientRequest.HttpMethod =="GET"){

        if (clientWebRequest.clientRequest.HttpMethod == "GET") {

           clientWebRequest.remoteWebServerRequest.Method = clientRequestMethod;

            clientWebRequest.remoteWebServerRequest.Method = clientRequestMethod;

           clientWebRequest.remoteWebServerRequest.ProtocolVersion

            clientWebRequest.remoteWebServerRequest.ProtocolVersion

               b =   clientWebRequest.clientRequest.ProtocolVersion;

                        =  clientWebRequest.clientRequest.ProtocolVersion;

           clientWebRequest.remoteWebServerRequest.UserAgent = clientWebRequest.clientRequest.UserAgent;

            clientWebRequest.remoteWebServerRequest.UserAgent = clientWebRequest.clientRequest.UserAgent;

           clientWebRequest.remoteWebServerRequest.Timeout = 30;

            clientWebRequest.remoteWebServerRequest.Timeout = 30;

           /* 遍历客户端响应标头键.一些

            /*  Iterate through the client response header keys.  Some

               * 我们感兴趣的键中的一个,可以添加一个通用的

                *  of the keys we are interested in and can be added with a generic

               *  header.add,其他要求我们向特定的公众讲话

                *  header.add, others require us to address a specific public

               * 属性,其他键设置在其他位置例如内容长度" */

                *  property, others keys are set elsewhere  such as 'Content-Length' */

           foreach(clientWebRequest.clientRequest.Headers.Keys中的字符串键){

            foreach (string key in clientWebRequest.clientRequest.Headers.Keys) {

                   ...(设置请求到Web服务器的所有标题)

                 ... (SETUP ALL THE HEADERS FOR THE REQUEST TO THE WEB SERVER)

           }

            }

           /* 从远程Web服务器获取响应*/

            /*  Get the response from the remote web server */

           clientWebRequest.remoteWebServerResponse =

            clientWebRequest.remoteWebServerResponse =

                          (HttpWebResponse)clientWebRequest.remoteWebServerRequest.GetResponse();

                  (HttpWebResponse)clientWebRequest.remoteWebServerRequest.GetResponse();

       }

        }

      否则((clientWebRequest.clientRequest.HttpMethod =="POST"){

        else if (clientWebRequest.clientRequest.HttpMethod == "POST") {

                        ...(类似于获取"

               ... (DO SOMETHING SIMILAR TO "GET"

       }

        }

}

private void _ProcessRemoteWebServerResponse(ref ClientWebRequest clientWebRequest){

private void _ProcessRemoteWebServerResponse(ref ClientWebRequest clientWebRequest) {

      尝试{

        try {

           /* 检查并确保来自远程服务器的响应*/

            /*  Check to make sure there was a response from the remote server  */

          如果(clientWebRequest.remoteWebServerResponse!= null){

            if (clientWebRequest.remoteWebServerResponse != null) {

               /* 设置客户的响应标题*/

                /*  Setup the Client's Response Headers */

               foreach(clientWebRequest.remoteWebServerResponse.Headers.Keys中的字符串键){

                foreach (string key in clientWebRequest.remoteWebServerResponse.Headers.Keys) {

               b ...(设置标题以发送给客户)

                      ... (SETUP HEADERS TO SEND TO CLIENT)

               }   /*   foreach(clientWebRequest.remoteWebServerResponse.Headers.Keys中的字符串键) */

                }   /*   foreach (string key in clientWebRequest.remoteWebServerResponse.Headers.Keys)   */

               _SendClientResponse(

                _SendClientResponse(

              ref clientWebRequest);                 

                    ref clientWebRequest);                 

           }           

            }            

       }

        }

       catch(ex ex例外){

        catch (Exception ex) {

               ...

                ...

       }

        }

      最终{

        finally {

           clientWebRequest.remoteWebServerResponse.Close();

            clientWebRequest.remoteWebServerResponse.Close();

       }

        }

}

private void _SendClientResponse(ref ClientWebRequest clientWebRequest){

private void _SendClientResponse(ref ClientWebRequest clientWebRequest) {

      流clientResponseOutputStream = null;

        Stream clientResponseOutputStream = null;

      尝试{

        try {

           const int BYTE_SIZE = 32;

            const int BYTE_SIZE = 32;

           /* 创建一个响应流对象*/

            /*  Create a response stream object */

          流responseStream =

            Stream responseStream =

               clientWebRequest.remoteWebServerResponse.GetResponseStream();

                clientWebRequest.remoteWebServerResponse.GetResponseStream();

           int bytesRead = 0;

            int bytesRead = 0;

           Byte []缓冲区=新的Byte [BYTE_SIZE];

            Byte[] buffer = new Byte[BYTE_SIZE];

           int bytesSent = 0;

            int bytesSent = 0;

           clientResponseOutputStream =

            clientResponseOutputStream =

               clientWebRequest.clientResponse.OutputStream;

                clientWebRequest.clientResponse.OutputStream;

           /*将响应二进制数据读入缓冲区*/

            /* Read the response binary data into a buffer  */

           bytesRead = responseStream.Read(buffer,0,32);

            bytesRead = responseStream.Read(buffer, 0, 32);

           while(bytesRead!= 0){

            while (bytesRead != 0) {

               bytesSent = bytesSent + bytesRead;

                bytesSent = bytesSent + bytesRead;

               clientResponseOutputStream.Write(buffer,0,bytesRead);

                clientResponseOutputStream.Write(buffer, 0, bytesRead);

               //阅读响应的下一部分                 

                // Read the next part of the response                 

               bytesRead = responseStream.Read(buffer,0,BYTE_SIZE);

                bytesRead = responseStream.Read(buffer, 0, BYTE_SIZE);

           }

            }

       }

        }

       catch(ex ex例外){

        catch (Exception ex) {

                        ...

               ...

       }

        }

      最终{

        finally {

          如果(clientWebRequest.clientResponse!= null){

            if (clientWebRequest.clientResponse != null) {

               clientResponseOutputStream.Flush();

                clientResponseOutputStream.Flush();

               clientResponseOutputStream.Close();

                clientResponseOutputStream.Close();

           }

            }

       }

        }

}


推荐答案

我正在尝试让熟悉此主题的人参与进来,以进一步研究该问题.

I am trying to involve someone familiar with this topic to further look at this issue. 

最好的问候


这篇关于为什么我的Http侦听器无法捕获来自Web服务器的所有响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 11:47