IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Dotnet Discussion :

Communication avec webservice [Débutant]


Sujet :

Dotnet

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Développeur .NET
    Inscrit en
    Février 2006
    Messages
    107
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Février 2006
    Messages : 107
    Par défaut Communication avec webservice
    Bonjour,

    Je dois développer une petite application (windows form) de test pour savoir si j'arrive à communiquer avec un webservice.

    J'arrive à rajouter le webservice dans mon projet sans problème, c'est après que ça se corse.

    Un dev m'a déposé un webservice, et m'a donné le wsdl :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    - <wsdl:types xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    - <xsd:schema>
      <xsd:import schemaLocation="V1.xsd1.xsd" namespace="http://WSSTestServiceLib/WSSTestOutboundService/V1" /> 
      </xsd:schema>
      </wsdl:types>
    - <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>
    + <wsdl:binding name="WSSTestOutboundServiceHttpBinding" type="ns0:WSSTestOutboundService">
      <soap11:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> 
    - <wsdl:operation name="getGreeting">
      <soap11:operation soapAction="http://WSSTestServiceLib/WSSTestOutboundService/V1/getGreeting" style="document" /> 
    - <wsdl:input name="getGreetingRequest">
      <soap11:body use="literal" /> 
      </wsdl:input>
    - <wsdl:output name="getGreetingResponse">
      <soap11:body use="literal" /> 
      </wsdl:output>
    - <wsdl:fault name="serviceErrors">
      <soap11:fault name="serviceErrors" use="literal" /> 
      </wsdl:fault>
      </wsdl:operation>
      </wsdl:binding>
    - <wsdl:service name="WSSTestOutboundServiceHttpService">
    - <wsdl:port name="WSSTestOutboundServiceHttpPort" binding="ns0:WSSTestOutboundServiceHttpBinding">
      <soap11:address location="http://XXXXX/TestSecurity/V1" /> 
      </wsdl:port>
    - <wsdl:port name="WSSTestOutboundServiceHttpPort.0" binding="ns0:WSSTestOutboundServiceHttpBinding">
      <soap11:address location="https://XXXXX/TestSecurity/V1" /> 
      </wsdl:port>
      </wsdl:service>
      </wsdl:definitions>
    Je n'arrive pas à comprendre comment je peux envoyer une demande et en recevoir, je suppose que tout se joue ici :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    - <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>
    Je ne comprends pas comment je fais pour lui envoyé le ns0:getGreetingRequestMsg et ensuite le recevoir.

    Voici mon code (qui ne veut rien dire pour le moment car j'essaye de comprendre comment cela fonctionne) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     local.WSSTestOutboundServiceHttpService CallWebService =
                    new local.WSSTestOutboundServiceHttpService();
     
                local.getGreeting test1 = new local.getGreeting();
     
                CallWebService.getGreetingAsync(test1);
                MessageBox.Show(test1.ToString());
                local.getGreetingResponse test2 = new local.getGreetingResponse();
     
                MessageBox.Show(test2.greeting);
    Je pense qu'il faut que je crée un xml et lui envoyer le getGreetingRequestMsg mais je ne vois pas comment.

    Help please!

    Merci.

  2. #2
    Membre confirmé
    Profil pro
    Développeur .NET
    Inscrit en
    Février 2006
    Messages
    107
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Février 2006
    Messages : 107
    Par défaut
    J'ai oublié de préciser que je suis avec le framework 4.0.

    Merci pour votre aide!

  3. #3
    Membre confirmé
    Profil pro
    Développeur .NET
    Inscrit en
    Février 2006
    Messages
    107
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Février 2006
    Messages : 107
    Par défaut
    *voix de leeloo dans le 5eme element*
    Please...help!

  4. #4
    Membre confirmé
    Profil pro
    Développeur .NET
    Inscrit en
    Février 2006
    Messages
    107
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Février 2006
    Messages : 107
    Par défaut
    J'ai pu le résoudre, en faites il n'y avait pas vraiment de fonction...plus une sorte de : dès que la communication se fait, on fait un "SOAP ACTION" qui envoi un xml et recoit un xml.
    Voilà ce que j'ai utilisé pour la réponse (donc pas d'ajout de Service request ou web request, tout en manuel) :
    http://www.roelvanlisdonk.nl/?p=1893

    Et voici mon code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
     
    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()
            {
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"http://xxxxxxx/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";
                return webRequest;
            }
        }

  5. #5
    Membre confirmé
    Profil pro
    Développeur .NET
    Inscrit en
    Février 2006
    Messages
    107
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Février 2006
    Messages : 107
    Par défaut
    Maintenant je dois faire fonctionne cela avec SSL.

    J'ai créé un certificat, je l'ai donné aux gars qui heberge le webservice pour qu'ils l'upload. Ils m'ont également donné leur certificat pour que je la rajoute dans ma racine de confiance.

    J'ai rajouté la ligne : ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

    Mais j'ai toujours l'exception :
    The request was aborted: Could not create SSL/TLS secure channel.

    Si quelqu'un sait ce qui se passe...

    Je continue de chercher...

    Voici mon code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    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=""https://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.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"https://xxxxx/TestSecurity/V1");
                webRequest.Headers.Add(@"SOAP:Action:""https://WSSTestServiceLib/WSSTestOutboundService/V1/getGreeting");
                webRequest.ContentType = "text/xml;charset=\"utf-8\"";
                webRequest.Accept = "text/xml";
                webRequest.Method = "POST";
                return webRequest;
            }

  6. #6
    Membre confirmé
    Profil pro
    Développeur .NET
    Inscrit en
    Février 2006
    Messages
    107
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Février 2006
    Messages : 107
    Par défaut
    Bon eh bien j'ai trouvé ma réponse...

    Avant d'utiliser ce code, il faut mettre les certificats dans notre store (pleins de sujets sur le net).

    Voici le code avec SSL (j'ai rajouté plusieurs certificat au cas où j'en louperais un) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
     
    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;
            }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Débutant] Communication avec un webservice
    Par sbouvetJD dans le forum ASP.NET
    Réponses: 9
    Dernier message: 10/06/2013, 10h25
  2. Communication avec un WebService
    Par yass_uk dans le forum Général JavaScript
    Réponses: 0
    Dernier message: 16/03/2012, 16h15
  3. [C#] Communication avec les ports parallèle et RS232
    Par jejerome dans le forum Windows Forms
    Réponses: 3
    Dernier message: 18/07/2004, 22h57
  4. [Débutant][Applet] Communication avec Servlet - IO Exception
    Par gandalf_le_blanc dans le forum Applets
    Réponses: 35
    Dernier message: 16/04/2004, 13h29
  5. [SSL]Communication avec un serveur en https
    Par menon dans le forum Développement Web en Java
    Réponses: 3
    Dernier message: 04/09/2003, 16h38

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo