首页 > 解决方案 > HttpWebRequest 500 内部错误,但 SOAPUI 有效

问题描述

我收到一个错误“远程服务器返回错误:(500) 内部服务器错误。” 状态码为“ProtocolError”

HttpWebRequest webRequest = null;

XmlDocument soapEnvelopeXml = new XmlDocument();
string requestEnvelopeString = SerializerHelper.ToRequestEnvelopeString(request);

soapEnvelopeXml.LoadXml(requestEnvelopeString);

webRequest = (HttpWebRequest)WebRequest.Create(<<endpointUrl>>);
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
webRequest.KeepAlive = true;
webRequest.ProtocolVersion = HttpVersion.Version11;

using (Stream stream = webRequest.GetRequestStream())
{
   soapEnvelopeXml.Save(stream);
}

try
{
   using (WebResponse webResponse = webRequest.GetResponse())
   {
      using (StreamReader reader = new StreamReader(webResponse.GetResponseStream()))
      {
         string responseEnvelopeString = reader.ReadToEnd();
      }
   }
}
catch (WebException ex)
{
   string exMessage = ex.Message;
}

当我直接通过 SOAPUI 发送 requestEnvelopeString 时,它可以工作,有人可以建议如何解决这个问题吗?

标签: soaphttpwebrequest

解决方案


发现问题是缺少 SOAP-ACTION 标头,SOAPUI 会自动添加到请求中。

谢谢薄荷灵魂


推荐阅读