Today we need to read all HTTP request headers in our client program, for some logging purposes. There is a stronger tool to read all headers, called Fiddler but it changes some proxy settings on the back end. From another hand, we cannot install this tool to our customer computers. So we need a managed solution to see all request headers from our client application (a windows application) to our web server (an xml web service).

Then i decided to adding a wrapper class for WSDL proxy, in front of the my web service helper class. My proxy class seems like that

public class ServiceProxy : MYService
    {

        protected override System.Net.WebRequest GetWebRequest(Uri uri)
        {
            //First event
            System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)base.GetWebRequest(uri);
            webRequest.KeepAlive = false;
            webRequest.ServicePoint.MaxIdleTime = 5000;
            webRequest.ServicePoint.ConnectionLimit = 16;
            webRequest.ServicePoint.Expect100Continue = false;
            webRequest.ServicePoint.UseNagleAlgorithm = false;

            return webRequest;
        }

        protected override object GetService(Type service)
        {
            return base.GetService(service);
        }

        public override System.Runtime.Remoting.ObjRef CreateObjRef(Type requestedType)
        {
            return base.CreateObjRef(requestedType);
        }

        protected override System.Xml.XmlReader GetReaderForMessage(System.Web.Services.Protocols.SoapClientMessage message, int bufferSize)
        {
            //Fourth event
            return base.GetReaderForMessage(message, bufferSize);
        }

        protected override WebResponse GetWebResponse(WebRequest request)
        {
            return base.GetWebResponse(request);
        }

        protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
        {
            //Third event
            if (Program.DEBUGMODE)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine(string.Format("URI: {0}", request.RequestUri.AbsoluteUri));
                sb.AppendLine(string.Format("Http Headers:"));
                foreach (string key in request.Headers.AllKeys)
                {
                    sb.AppendLine(string.Format("{0} = {1}", key, request.Headers[key]));
                }
                DebugWriter.WriteDebug("ServiceProxy.GetWebResponse", sb.ToString());
            }

            return base.GetWebResponse(request, result);
        }

        protected override System.Xml.XmlWriter GetWriterForMessage(System.Web.Services.Protocols.SoapClientMessage message, int bufferSize)
        {
            //Second event
            return base.GetWriterForMessage(message, bufferSize);
        }

        public override object InitializeLifetimeService()
        {
            return base.InitializeLifetimeService();
        }
    }

And here is the my debug data

 Date : 3/21/2014 3:30:12 PM
 Method : SYPServiceProxy.GetWebRequest
 Data :
 URI: http://my.myservice.com/myservice/myservice.asmx
 Http Headers:
 User-Agent = Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 2.0.50727.3634)
 Content-Type = text/xml; charset=utf-8
 SOAPAction = "http://www.myservice.com/GetMyData"
 Host = my.myservice.com
 Content-Length = 367
 Connection = Close

Happy coding..

If you like this, follow my RSS channel!