如何监听web浏览器发送到服务器的请求

本文关键字:服务器 请求 浏览器 web 何监听 监听 | 更新日期: 2023-09-27 18:17:31

我想在我的程序听web浏览器请求发送到服务器,然后服务器,响应此请求。但在我的客户端代码不能发送请求后Listener.start()我在异常中有这样的消息:"参数不正确"

' try
    {
       // IPHostEntry ipHostInfo = Dns.Resolve("192.168.1.2");//GetHostAddresses(string "127.0.0.1");//Resolve("host.contoso.com");
     //   IPAddress ipAddress = ipHostInfo.AddressList[0];
        IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
        Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        client.BeginConnect(remoteEP,new AsyncCallback(ConnectCallback), client);
        connectDone.WaitOne();
        HttpListener listener = new HttpListener();
        listener.Prefixes.Add("http://127.0.0.1: 2000/");
        listener.Start();
        Console.WriteLine("im listening...");
        HttpListenerContext context = listener.GetContext();
        HttpListenerRequest requst= context.Request;
        Send(client,requst.ToString());
        sendDone.WaitOne();`

如何监听web浏览器发送到服务器的请求

一段时间以前,我为此写了一个函数:

public string request(string url)
    {
        string responseText = "";
        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        WebHeaderCollection header = response.Headers;
        var encoding = ASCIIEncoding.ASCII;
        using (var reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
        {
            responseText = reader.ReadToEnd();
        }
        return responseText;
    }

这不是对你的代码的修正,但可能对你有用。只要把你的http://127.0.0.1: 2000/作为参数,就可以了。