Click here to Skip to main content
14,771,671 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to reduce the slowness while fetching response using httpwebresponse with stream reader.
i am using asp.net with c# in higher framework (4.6.1) too.
I am getting a delay of around 30 seconds and overall one minute while fetching the response XML from the core using soap header. In Soap UI there is no slowness seen.
I am using the serivcemanager for which secure protocol used is TLS1.2 and ServerCertificateValidationCallback to which SSL certificates are added,

Tried reducing slowness while detecting default proxy
<system.net>
  <defaultProxy enabled="false">
    <proxy/>
    <bypasslist/>
    <module/>
  </defaultProxy>
</system.net>


What I have tried:

My sample code is as below:
  CertificateOverride oCertOverride = new CertificateOverride();
            ServicePointManager.ServerCertificateValidationCallback = oCertOverride.RemoteCertificateValidationCallback;

            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

            const SslProtocols _Tls12 = (SslProtocols)0x00000C00;
            const SecurityProtocolType Tls12 = (SecurityProtocolType)_Tls12;
            ServicePointManager.SecurityProtocol = Tls12;

           

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(AccountStatusLookUpURL);
           
            
            request.Headers.Add("SOAPAction", "https://soa.nbf.ae/ICCSAccountLookup/ICCSAccountLookup");
            request.ContentType = "text/xml; charset=\"utf-8\"";
            request.Accept = "gzip,deflate";
            request.Method = "POST";

            request.Proxy = new WebProxy();
            request.ProtocolVersion = HttpVersion.Version10;

          
            request.ClientCertificates.Add(new X509Certificate2(Common.CORE_PFXPath, Common.CORE_PFXPassword));


                
using (Stream stm = request.GetRequestStream())
            {
                using (StreamWriter stmw = new StreamWriter(stm))
                {
                    stmw.Write(oRequest);
                    LogCORE("Request Posted successfully for Account Status Lookup API for AccountNumber : " + AccountNumber + ". Request : " + oRequest, 2);
                }
            }

HttpWebResponse httpWebRes = (HttpWebResponse)request.GetResponse();
using (StreamReader responseReader = new StreamReader(httpWebRes.GetResponseStream()))
                {
                    
                    result = responseReader.ReadToEnd();
                  
                    //XmlElement root;
                    if (!string.IsNullOrEmpty(result.Trim()))
                    {

                        XmlDocument doc = new XmlDocument();
                        XDocument xdoc = new XDocument();
                        doc.LoadXml(result);

                       
                }
Posted
Updated 19-Aug-20 4:28am

1 solution

When you're setting up the request, delete your proxy setup ie
request.Proxy = new WebProxy();
add the line in bold as indicated
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(AccountStatusLookUpURL);
           
            
            request.Headers.Add("SOAPAction", "https://soa.nbf.ae/ICCSAccountLookup/ICCSAccountLookup");
            request.ContentType = "text/xml; charset=\"utf-8\"";
            request.Accept = "gzip,deflate";
            request.Method = "POST";

            request.ProtocolVersion = HttpVersion.Version10;
          
            request.ClientCertificates.Add(new X509Certificate2(Common.CORE_PFXPath, Common.CORE_PFXPassword));
request.Proxy = GlobalProxySelection.GetEmptyWebProxy(); // null;
If you dont use a proxy or don't set the proxy type the request will try to auto-detect and then configure a proxy ...
   
Comments
ranio 19-Aug-20 9:54am
   
response is coming fine for me but only after a delay as mentioned in my query. so are you telling setting request.proxy as you told will reduce the response delay.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900