0

我正在尝试使用 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>
4

1 回答 1

2

你到底为什么要手动构建请求?

当您添加服务引用时,Visual Studio 所做的是生成一个实现服务客户端的类 - 为您完成所有这些工作。

您可以在创建服务引用时指定的命名空间中找到客户端。

例如,如果您指定了 Svc 命名空间,您可能会在以下位置找到您的客户端Svc.SiteAuth

所以使用它如下:

var client = new Svc.SiteAuth();
Svc.GetUserResult response = client.GetUser(1);

response将是一个填充有服务设置的属性(PWordAgency和所有其他...)的实例。

于 2012-07-13T22:48:48.960 回答