I am facing an issue where my Web API is working fine on local systems but creating issues when deployed on server. I have checked, cross-checked multiple times to see if I have missed anything from the config, but everything is in order. Below is the code I am using. The line throwing this error is: using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
public static string PostDataToWebService(string stream, string CustIP)
{
var _url = AdditionalSetting.AutoEuroURL;
string soapResult = string.Empty;
try
{
XmlDocument soapEnvelopeXml = CreateSoapEnvelope(stream);
HttpWebRequest webRequest = CreateWebRequest(_url, CustIP);
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
asyncResult.AsyncWaitHandle.WaitOne();
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
{
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
soapResult = rd.ReadToEnd();
}
}
}
catch (WebException wbex)
{
using (var ResStream = wbex.Response.GetResponseStream())
using (var reader = new StreamReader(ResStream))
{
ErrorLog.ErrorLogs("WebException at AutoEurope Call web service : " + reader.ReadToEnd());
}
}
return soapResult;
}
private static XmlDocument CreateSoapEnvelope(string stream)
{
XmlDocument soapEnvelop = new XmlDocument();
try
{
soapEnvelop.LoadXml(stream);
}
catch (Exception ex)
{
}
return soapEnvelop;
}
private static HttpWebRequest CreateWebRequest(string url, string CustIP)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("SOAPAction", "OTA");
webRequest.Headers.Add("X-Forwarded-For", "\"" + CustIP + "\"");
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
webRequest.KeepAlive = false;
webRequest.ProtocolVersion = HttpVersion.Version10;
return webRequest;
}
private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
}
The CustIP I am getting is the request IP address which I am getting as a parameter in my method. It is in a proper format. Any suggestion would be helpful.