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.
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.");
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";
byte[] postBytes = Encoding.ASCII.GetBytes(post_data);
Stream requestStream = null;
try
{
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();
}
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.