goatman
goatman

Reputation: 13

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.

I must develop a simple windows form application which can consume a simple webservice, i have only the wsdl from webservice. I am using framework 4. I successfully added the webservice into my project with no problem. 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 :

    - <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.

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

Upvotes: 0

Views: 8545

Answers (2)

goatman
goatman

Reputation: 13

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.

After that i wanted to do it with SSL connection and certificate from store.

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

Upvotes: 1

user2755680
user2755680

Reputation: 95

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.

Upvotes: 0

Related Questions