尝试使用Web服务但没有响应请求返回数据

时间:2012-07-13 22:41:09

标签: c# web-services no-data

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

1 个答案:

答案 0 :(得分:2)

为什么要手动构建请求?

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

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

例如,如果您指定了Svc的命名空间,则可以在Svc.SiteAuth

找到您的客户端

因此请按如下方式使用:

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

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