我面临一个问题,我的 Web API 在本地系统上运行良好,但在服务器上部署时会产生问题。我已经检查,交叉检查了多次,看看我是否遗漏了配置中的任何内容,但一切都井井有条。下面是我正在使用的代码。引发此错误的行是: 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);
}
}
我得到的 CustIP 是我在我的方法中作为参数得到的请求 IP 地址。它的格式正确。任何建议都会有所帮助。