下面这段代码,在别的电脑就能运行,在我自己的电脑上就不行,不知道是什么原因,找了好久也没找到原因,请各位大佬帮帮忙,不行只能做系统了.
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& 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()
500是对方服务器内部错误,也就是说你的参数可能不正确造成了对方处理的错误。
也不排除,对方故意对你的ip地址做出了限制。
这个你最好抓包和浏览器的啼叫对比下,看看有何不同。
(500) 内部服务器错误 是对方的错误 如果能让对方查一下看看是什么原因最好,不能让对方提供信息的话,那就抓包看看
看看浏览器的正常的数据包和你的程序的错误的数据包 有什么不一样
https://www.cnblogs.com/cresuccess/archive/2009/12/09/1619977.html
用wireshark对比一下
去别人那里收集一点数据 和自己的对比一下