我正在尝试使用 Web 服务,但由于某种原因,我的请求没有返回任何数据。我创建了对 Web 服务的服务引用。这是我的代码:
static void Main(string[] args)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://127.0.0.1/MyWS/MyService.asmx");
request.Headers.Add(@"SOAPAction", "\"http://tempuri.org/GetUser");
request.ContentType = "text/xml;charset=\"utf-8\"";
request.Accept = "text/xml";
request.Method = "POST";
request.ContentLength = 8000;
XmlDocument soapEnvelopeXml = new XmlDocument();
soapEnvelopeXml.LoadXml(@"<?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:Header>
<ServiceAuthHeader xmlns=""http://tempuri.org/"">
<SiteName>MySiteName</SiteName>
<Password>password</Password>
<AgencyName>MyDept</AgencyName>
<UserName>Mark</UserName>
<ApplicationName>CSharpApp</ApplicationName>
<DatabaseName>DBName</DatabaseName>
</ServiceAuthHeader>
</soap:Header>
<soap:Body>
<GetUser xmlns=""http://tempuri.org/"">
<UsrNum>1</UsrNum>
</GetUser>
</soap:Body>
</soap:Envelope>");
using (Stream stream = request.GetRequestStream())
{
using (StreamWriter streamWriter = new StreamWriter(stream))
{
streamWriter.Write(soapEnvelopeXml);
}
}
WebResponse response = request.GetResponse();
}
这是我的请求的格式:
POST /MyWS/MyService.asmx HTTP/1.1
Host: 127.0.0.1
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/GetUser"
<?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:Header>
<ServiceAuthHeader xmlns="http://tempuri.org/">
<SiteName>string</SiteName>
<Password>string</Password>
<AgencyName>string</AgencyName>
<UserName>string</UserName>
<ApplicationName>string</ApplicationName>
<DatabaseName>string</DatabaseName>
</ServiceAuthHeader>
</soap:Header>
<soap:Body>
<GetUser xmlns="http://tempuri.org/">
<UsrNum>int</UsrNum>
</GetUser>
</soap:Body>
</soap:Envelope>
以下是返回响应的方式:
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?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>
<GetUserResponse xmlns="http://tempuri.org/">
<GetUserResult>
<Usrnum>int</Usrnum>
<UsrTitle>string</UsrTitle>
<Userfnam>string</Userfnam>
<Userlnam>string</Userlnam>
<UserName>string</UserName>
<Agtnum>int</Agtnum>
<Unit>string</Unit>
<Dbnum>int</Dbnum>
<UsrGrpName>string</UsrGrpName>
<PWord>string</PWord>
<UsrTs>int</UsrTs>
<IconColor>string</IconColor>
<IconStyle>string</IconStyle>
<ShortUserName>string</ShortUserName>
<UsrContactPhone>string</UsrContactPhone>
<UsrContactEmail>string</UsrContactEmail>
<Agency>string</Agency>
</GetUserResult>
</GetUserResponse>
</soap:Body>
</soap:Envelope>