Proxy Server unable to trace http requests to locally hosted Web Applications RRS feed

  • Question

  • I am trying to build a Proxy server that can intercept ll http requests from browser to any Web App(Either local or over internet). My Proxy Serve ris able to handle requests to Internet but unable to detect the http requests to local web apps hosted on IIS.

    Here is the Code

    using System;
    using System.Collections.Generic;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading;
    namespace SimpleHttpProxy
        class ClientConnection
            private Socket clientSocket;
            public ClientConnection(Socket client)
                this.clientSocket = client;
            public void StartHandling()
                Thread handler = new Thread(Handler);
                handler.Priority = ThreadPriority.AboveNormal;
            private void Handler()
                bool recvRequest = true;
                string EOL = "\r\n";
                string requestPayload       = "";
                string requestTempLine      = "";
                List<string> requestLines   = new List<string>();
                byte[] requestBuffer        = new byte[1];
                byte[] responseBuffer       = new byte[1];
                    //State 0: Handle Request from Client
                    while (recvRequest)
                        string fromByte = ASCIIEncoding.ASCII.GetString(requestBuffer);
                        requestPayload += fromByte;
                        requestTempLine += fromByte;
                        if (requestTempLine.EndsWith(EOL))
                            requestTempLine = "";
                        if (requestPayload.EndsWith(EOL + EOL))
                            recvRequest = false;
                    Console.WriteLine("Raw Request Received...");
                    //State 1: Rebuilding Request Information and Create Connection to Destination Server
                    string remoteHost = requestLines[0].Split(' ')[1].Replace("http://", "").Split('/')[0];
                    string requestFile = requestLines[0].Replace("http://", "").Replace(remoteHost, "");
                    requestLines[0] = requestFile;
                    requestPayload = "";
                    foreach (string line in requestLines)
                        requestPayload += line;
                        requestPayload += EOL;
                    Socket destServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    destServerSocket.Connect(remoteHost, 80);
                    //State 2: Sending New Request Information to Destination Server and Relay Response to Client
                    //Console.WriteLine("Begin Receiving Response...");
                    while (destServerSocket.Receive(responseBuffer) != 0)
                catch (Exception e)
                    Console.WriteLine("Error Occured: " + e.Message);

    Is it recommended to use our own Proxy Server or use some already available ones like SQUID etc.. if yes, kindly let me know which is easier for starters like me.



    Saturday, April 19, 2014 7:42 PM


  • When a connection is made to a server there is a negotiation that occurs between the client and server.  The webpage http headers are used as part of the negotiation to find a compatible mode of transfer.  the headers on the IIS server are different from the internet page that works.

    You probably have to add http headers to you application to allow your application to work with more types of servers where the headers do not match.  Normally a good response will return with a status "200 Done".  When you don't get a good response a different status will be returned.

    My normal recommendation is to use a sniffer like wireshark or fiddler to trace the connection and then verify the HTTP headers to determine the root cause of the issue.  I also recommend trying to connect using an IE browser to see if you can manually connect to the server. If the IE works then try comparing a trace with the IE with a trace with your VS application to determine the differences.

    Make sure you delete the cookies by deleting the history inside your IE application between traces with the sniffer.  Your VS application will use the same cookies returned from the IE.  You may find that running the IE and then running your application your application will work.  then deleting the cookies your application will fail, but the IE will work.


    Saturday, April 19, 2014 9:52 PM