25

使用 HttpWebRequest 调用WebService 就报 远程服务器返回错误: (500) 内部服务器错误。

下面这段代码,在别的电脑就能运行,在我自己的电脑上就不行,不知道是什么原因,找了好久也没找到原因,请各位大佬帮帮忙,不行只能做系统了.

string postDataStr = "发送报文";
string Url = "地址";
Encoding encoding = Encoding.UTF8;
byte[] postData = encoding.GetBytes(postDataStr);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
//request.ContentType = "application/x-www-form-urlencoded";
request.ContentType = "text/xml";
request.ContentLength = postData.Length;
Stream myRequestStream = request.GetRequestStream();
myRequestStream.Write(postData, 0, postData.Length);
myRequestStream.Close();
//报错行
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, encoding);
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;

直接错误原因:System.Net.WebException:“远程服务器返回错误: (500) 内部服务器错误。”

这是提取的错误:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Header>
    <soap12:Upgrade xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
      <soap12:SupportedEnvelope qname="soap:Envelope"></soap12:SupportedEnvelope>
      <soap12:SupportedEnvelope qname="soap12:Envelope"></soap12:SupportedEnvelope>
    </soap12:Upgrade>
  </soap:Header>
  <soap:Body>
    <soap:Fault>
      <faultcode>soap:VersionMismatch</faultcode>
      <faultstring>System.Web.Services.Protocols.SoapException: SOAP 版本可能不匹配: 出现意外的 Envelope 命名空间 。应为 http://schemas.xmlsoap.org/soap/envelope/。 在 System.Web.Services.Protocols.SoapServerProtocol.CheckHelperVersion() 在 System.Web.Services.Protocols.SoapServerProtocol.Initialize() 在 System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean&amp; abortProcessing)</faultstring>
      <detail/>
    </soap:Fault>
  </soap:Body>
</soap:Envelope>

用SoapUI 调用自己的webService 就没事.

当我固定在地址固定某个接口的时候就会报这个错,但是我的格式就是
request.ContentType = "text/xml";

System.InvalidOperationException: 请求格式无效: text/xml。
在 System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
在 System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
查看全部
Spaca
Spaca
2020/07/27 22:08
  • c#
  • microsoft
  • visual studio
  • 点赞
  • 收藏
  • 回答
    私信

4个回复