My webrequest timeout does not seem to be working. The URL that host has some TLS setting issue. But even in cases of exceptions, is it not expected to respect the timeout? The hostname has the domain name and not IP.Please let me know what I am missing for the timeout to work at 30secs immaterial of success/exception scenarios. Find the code below - we have currently kept the timeout to be 30s but recieving the response after 1.15mins or more.
public string CallJsonService(string JsonString, string ServiceURL, string strLogUser, string RandomValue, string strAPIToCall)
{
string displayvalue = "";
try
{
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => { return true; };
ServicePointManager.Expect100Continue = false;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
byte[] bytestream = Encoding.UTF8.GetBytes(JsonString);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(ServiceURL);
req.Method = "POST";
req.ContentType = "application/json; charset=utf-8";
req.ContentLength = bytestream.LongLength;
req.Timeout = 30000;
req.ReadWriteTimeout = 30000;
req.KeepAlive = true;
String ProxyValue = objCommon.GetParamValue("ProxyValueForInstant");
req.Proxy = new System.Net.WebProxy(ProxyValue, true);
req.Headers.Add("vRanKey", Convert.ToString(RandomValue));
req.Accept = "application/json";
using (Stream stream = req.GetRequestStream())
{
stream.Write(bytestream, 0, bytestream.Length);
stream.Flush();
}
using (WebResponse responserequest = req.GetResponse())
{
Stream ResponseStream = responserequest.GetResponseStream();
displayvalue = HttpUtility.HtmlDecode((new StreamReader(ResponseStream)).ReadToEnd());
}
}
catch (WebException e)
{
using (WebResponse response = e.Response)
{
HttpWebResponse httpResponse = (HttpWebResponse)response;
if (response != null)
{
using (Stream data = response.GetResponseStream())
{
using (var reader = new StreamReader(data))
{
displayvalue = reader.ReadToEnd();
}
}
}
else
{
throw e;
}
}
}
catch (Exception ex)
{
throw ex;
}
return displayvalue;
}