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>