简体   繁体   English

仅使用wsdl在C#中使用Web服务

[英]Consume webservice in c# with only wsdl

I am new in c# and webservice, i did a ton of research and i am still stuck. 我是C#和Web服务的新手,我进行了大量研究,但仍然遇到困难。

I must develop a simple windows form application which can consume a simple webservice, i have only the wsdl from webservice. 我必须开发一个可以使用简单Web服务的简单Windows窗体应用程序,我只有Web Service的wsdl。 I am using framework 4. I successfully added the webservice into my project with no problem. 我正在使用框架4。我已成功将Web服务成功添加到我的项目中。 I just cannot know how i must call methods with inputs and outputs, i am not sure that methods are actually called... 我只是不知道我该如何调用带有输入和输出的方法,我不确定方法实际上是否被调用...

I think that the key point is in the porttype : 我认为关键是在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>

I don't know how to do inputs with my program with only this, i think it is in xml but i don't know how to do it. 我不知道该如何用我的程序进行输入,我认为它是在xml中,但我不知道该怎么做。

Here is my code which signify absoluterly nothing because it is just for testing : 这是我的代码,绝对没有任何意义,因为它仅用于测试:

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);

I would just check in the object browser you are sending all the write parameters making sure you are passing exactly what it wants. 我只需要在对象浏览器中检查一下您是否正在发送所有写参数,以确保您准确传递了它想要的内容。

If you are not sure that the service is actually being called why not just 如果您不确定该服务实际上正在被调用,为什么不只是

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

See if it returns anything to response. 查看它是否返回任何响应。 If it does you should have some idea where to go from there. 如果是这样,您应该知道从那里去哪里。 Just a guess. 只是一个猜测。

So after multiple try i found all my answers. 因此,经过多次尝试,我找到了所有答案。

In fact, i am making a connection to webservice sending a xml (and aking the method getGreeting) and receiving a xml in response. 实际上,我正在建立与Web服务的连接,以发送xml(并使用getGreeting方法)并接收xml作为响应。

After that i wanted to do it with SSL connection and certificate from store. 之后,我想使用SSL连接和来自商店的证书来完成此操作。

Here is the result of the code i needed, it works perfectly : 这是我需要的代码的结果,它可以完美地工作:

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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM