该Web服务可与Postman一起使用。 我正在尝试使用WebRequest调用该服务,但它给了我500内部服务器错误。 我的Web服务托管在IIS中。 当添加为C#项目的服务引用时,可以正常工作,但不能与以下代码一起使用。 Web服务是一个简单的空asp.net项目,我向其中添加了一个.asmx文件并定义了web方法。 当我尝试挖死神时,我也确实得到了以下错误。
服务器无法处理请求。 ---> system.xml.XmlException: 根级别的数据无效。第1行,位置
下面是示例网址: http://XXXXX:port/Service.asmx?op=SendMessage
示例SOAP
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<SendMessage xmlns="http://tempuri.org/">
<message>
<To>string</To>
<Body>string</Body>
<SID>string</SID>
<Token>string</Token>
<Number>string</Number>
</message>
</SendMessage>
</soap:Body>
</soap:Envelope>
///这是我调用的方法,该方法在GetResponse()处引发错误。
protected T Post<T>(string url,string contentType,object data = null)
{
try
{
WebRequest webRequest= WebRequest.Create(url);
string action = "http://tempuri.org/";
webRequest.Headers.Add("SOAPaction",action);
webRequest.ContentType = contentType;
byte[] serializedData = SerializeBody(data);
using (Stream requestStream = webRequest.GetRequestStream())
{
requestStream.Write(serializedData,serializedData.Length);
}
T result;
using (webresponse webresponse = webRequest.GetResponse())
{
result = DeserializeResponse<T>(webresponse);
}
return result;
}
catch (WebException ex)
{
throw HandleCommonCases(ex,url,data);
}
}