仅使用wsdl在c#中使用webservice

时间:2014-02-03 19:31:32

标签: c# wcf web-services input

我是c#和webservice的新手,我做了很多研究,但我仍然陷入困境。

我必须开发一个简单的Windows窗体应用程序,它可以使用简单的Web服务,我只有来自webservice的wsdl。我正在使用框架4.我成功地将webservice添加到我的项目中没有任何问题。我只是不知道我必须如何使用输入和输出调用方法,我不确定方法是否实际被调用...

我认为关键点在于porttype:

    - <wsdl:message name="getGreetingRequestMsg">
  <wsdl:part name="getGreetingParameters" element="xsns:getGreeting" xmlns:xsns="http://WSSTestServiceLib/WSSTestOutboundService/V1" /> 
  </wsdl:message>
- <wsdl:message name="getGreetingResponseMsg">
  <wsdl:part name="getGreetingResult" element="xsns:getGreetingResponse" xmlns:xsns="http://WSSTestServiceLib/WSSTestOutboundService/V1" /> 
  </wsdl:message>
- <wsdl:portType name="WSSTestOutboundService">
- <wsdl:operation name="getGreeting">
  <wsdl:input name="getGreetingRequest" message="ns0:getGreetingRequestMsg" /> 
  <wsdl:output name="getGreetingResponse" message="ns0:getGreetingResponseMsg" /> 
  <wsdl:fault name="serviceErrors" message="ns1:serviceErrorsMsg" /> 
  </wsdl:operation>
  </wsdl:portType>

我不知道如何使用我的程序对我的程序进行输入,我认为它是在xml中,但我不知道该怎么做。

这是我的代码,它绝对没有任何意义,因为它仅用于测试:

myws.WSSTestOutboundServiceHttpService CallWebService =
                new myws.WSSTestOutboundServiceHttpService();

myws.getGreeting test1 = new myws.getGreeting();

CallWebService.getGreetingAsync(test1);
MessageBox.Show(test1.ToString());
myws.getGreetingResponse test2 = new myws.getGreetingResponse();

MessageBox.Show(test2.greeting);

2 个答案:

答案 0 :(得分:0)

我只是在对象浏览器中检查您发送的所有写入参数,确保您正确地传递它想要的内容。

如果您不确定该服务是否实际被调用,为什么不只是

var Response = CallWebService.Function(test1, test2, test3);

查看它是否返回任何响应。如果是的话,你应该知道从那里去哪里。只是一个猜测。

答案 1 :(得分:0)

经过多次尝试,我找到了所有的答案。

事实上,我正在建立一个连接webservice发送一个xml(和方法getGreeting)并接收一个xml作为响应。

之后我想用SSL连接和商店证书来做。

以下是我需要的代码的结果,它完美运行:

private void button1_Click(object sender, EventArgs e)
    {
        HttpWebRequest request = CreateWebRequest();
        XmlDocument soapEnvelopeXml = new XmlDocument();
        soapEnvelopeXml.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?>
            <soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:v1=""http://WSSTestServiceLib/WSSTestOutboundService/V1"">
            <soapenv:Header/>
            <soapenv:Body>
                <v1:getGreeting/>
            </soapenv:Body>
            </soapenv:Envelope>");

        using (Stream stream = request.GetRequestStream())
        {
            soapEnvelopeXml.Save(stream);
        }

        using (WebResponse response = request.GetResponse())
        {
            using (StreamReader rd = new StreamReader(response.GetResponseStream()))
            {
                string soapResult = rd.ReadToEnd();
                MessageBox.Show(soapResult);
            }
        }
    }
    /// <summary>
    /// Create a soap webrequest to [Url]
    /// </summary>
    /// <returns></returns>
    public HttpWebRequest CreateWebRequest()
    {
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
        ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"https://www.XXXXX.com/TestSecurity/V1");
        webRequest.Headers.Add(@"SOAP:Action:""http://WSSTestServiceLib/WSSTestOutboundService/V1/getGreeting");
        webRequest.ContentType = "text/xml;charset=\"utf-8\"";
        webRequest.Accept = "text/xml";
        webRequest.Method = "POST";


        string certificateName = "name of certificate";
        X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
        store.Open(OpenFlags.ReadOnly);
        X509Certificate2Collection certificates = store.Certificates.Find(X509FindType.FindBySubjectName, certificateName, true);
        foreach (X509Certificate certificate in certificates)
        {
            webRequest.ClientCertificates.Add(certificate); 
        }
        certificateName = "name of certificate";
        certificates = store.Certificates.Find(X509FindType.FindBySubjectName, certificateName, true);
        foreach (X509Certificate certificate in certificates)
        {
            webRequest.ClientCertificates.Add(certificate);
        }
        return webRequest;
    }