Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have access to a simple WebService written in PHP that returns the IP address of the Client. I am using this WS in my C# application using HttpWebRequest & HttpWebResponse. This WS takes a long tiem to receive the reply from server. Actually according to server logs, it returns fast from server also, but by the time my client receives it, time is occupied over there.

I am accessing this WS 4-5 times in my application and sue to this delay in response my client takes time to accomplish the goal. I use other WS's also from the same server and it works perfectly fine.

Can anyone help me identify where must be the problem - on Server or Cleint. What can be the problem that delays response for specific WS.

NOTE : I call same function to send/receive response from the server for all WS's that I use. I have the logs & code of the client.


C#
public static string getPublicIP(String host)
{
     string post_data = "cmd=ipcheck";
     string uri = "https://" + host;

     String responseData = "";

     CommonUtilities.WriteLog("Into getPublicIP(host)... URI = " + uri);
     try
     {
       CommonUtilities.WriteLog("Into getPublicIP(host)...CALLING SENDPOST.");
       // Performs POST on uri & post_data
       responseData = SendPost(uri, post_data);
       CommonUtilities.WriteLog("Into getPublicIP(host)...RCVD RESPONSE");
       responseData = responseData.Trim();
       if (responseData.Length <= 0)
          throw new Exception("No response received from Server for IP Check");
     }
     catch (Exception e)
     {
         Console.WriteLine("********* Excp in getPublicIP MSG - " + e.Message);
         Console.WriteLine("INNER EXcep = " + e.InnerException);
         throw new Exception(e.Message, e.InnerException);
     }

     int start = responseData.Contains("<ip>") ? responseData.IndexOf("<ip>") +4 : -1;
     int end = responseData.Contains("</ip>") ? responseData.IndexOf("</ip>") : -1;
     string data = String.Empty;

     if (start != -1 && end != -1)
     {
        data = responseData.Substring(start, end - start);
     }
     CommonUtilities.WriteLog("Into getPublicIP(host)...DATA RETURNED = " + data);
     return data;
}

        private static String SendPost(String uri, String post_data)
        {
            String resData = "";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.KeepAlive = false;
            request.ProtocolVersion = HttpVersion.Version10;
            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "POST";

            // turn request string into byte[]
            byte[] postBytes = Encoding.ASCII.GetBytes(post_data);

            Stream requestStream = null;

            try
            {
                // Send it
                request.ContentLength = postBytes.Length;
                CommonUtilities.WriteLog("Request URL = " + request.RequestUri.ToString() + " DATA = " + post_data);
                requestStream = request.GetRequestStream();
                requestStream.Write(postBytes, 0, postBytes.Length);
                CommonUtilities.WriteLog("INTO SendPost: REQUEST SEND SUCCESSFULLY");
            }
            catch (WebException we)
            {
               // .......
            }
            finally
            {
                if (requestStream != null)
                    requestStream.Close();
            }

            // Get the response
            HttpWebResponse response = null;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
                if (response == null)
                    return "";
                CommonUtilities.WriteLog("INTO SendPost: GOT RESPONSE");
                StreamReader sr = new StreamReader(response.GetResponseStream());
                resData = sr.ReadToEnd().Trim();
                sr.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Excep THROWN From SendPost 'GET' try block");
                throw new Exception("Error receiving response from POST : " + e.Message, e.InnerException);
            }
            finally
            {
                if (response != null)
                    response.Close();
            }
    
            return resData;
        }        


LOGS :
12-03-2012 15:22:39:815 ==> INFO : Calling getPublicIP from FORM...
12-03-2012 15:22:39:817 ==> INFO : Into getPublicIP(host)... 
12-03-2012 15:22:39:817 ==> INFO : Into getPublicIP(host)...CALLING SENDPOST.
12-03-2012 15:22:40:744 ==> INFO : INTO SendPost: REQUEST SEND SUCCESSFULLY
12-03-2012 15:22:41:422 ==> INFO : INTO SendPost: GOT RESPONSE
12-03-2012 15:22:41:424 ==> INFO : Into getPublicIP(host)...RCVD RESPONSE
12-03-2012 15:22:41:425 ==> INFO : Into getPublicIP(host)...DATA RETURNED = 

......
12-03-2012 15:23:34:387 ==> INFO : Checking Connectivity...
12-03-2012 15:23:34:389 ==> INFO : Into getPublicIP(host)... 
12-03-2012 15:23:34:390 ==> INFO : Into getPublicIP(host)...CALLING SENDPOST.
12-03-2012 15:23:51:230 ==> INFO : INTO SendPost: REQUEST SEND SUCCESSFULLY
12-03-2012 15:23:53:423 ==> INFO : INTO SendPost: GOT RESPONSE
12-03-2012 15:23:53:424 ==> INFO : Into getPublicIP(host)...RCVD RESPONSE
12-03-2012 15:23:53:426 ==> INFO : Into getPublicIP(host)...DATA RETURNED = 


At a stage the difference between CALLING SENDPOST & RCVD RESPONSE is upto 15secs. What can be the reason the 2nd time huge difference in SendPost() for sending the request.

Please try to help me out at the earliest. The delay is affecting the performance of the application which is affecting my performance.

Any help is highly appreciated. Thanks.
Posted
Updated 12-Mar-12 0:09am
v2
Comments
Herman<T>.Instance 12-Mar-12 4:18am    
the question is...what code runs the WS? Is any logging available on the server where the WS resides?

You need to investigate on the service end rather than the client. You may need to put in logging and tracing in the services to see what exactly is taking this much time.
 
Share this answer
 
Comments
All Time Programming 12-Mar-12 6:13am    
Hello, To make sure at the client side, I added 1 more comment while the request to the server is sent in SendPost(). I see there the duration is longer. In the logs, see the 2nd one in bold, there the duration to actually sent the POST is delayed. What can be the reason for that.

REQEST: Kindly see the question, have added code for "SendPost()" and new logs.

Hoping to find the cause at the earliest.
I just added
C#
request.Proxy = null;


and this did the magic. It might be looking for proxy (auto-detecting). Just 1 wonder : how come this auto-detecting proxy is looking only for this WS and not for others ????

But anyways, this worked out for me.

Thanks to all of you.
 
Share this answer
 

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