C#:我想创建一个简单的SOAP客户端

时间:2018-04-12 08:59:24

标签: c# authentication soap

我需要通过证书或更好的密钥库文件(myfile.p12)进行客户端身份验证。

我在SoapUI中使用过它并且在那里工作了。我收到了关于可用请求的信息。

如果我尝试向我的空项目添加服务引用,它总是会失败。

There was an error downloading 'https://MYURL/api?wsdl/$metadata'.The request was aborted: Could not create SSL/TLS secure channel. Metadata contains a reference that cannot be resolved: 'https://MYURL/soapWebService/api?wsdl'. Could not establish secure channel for SSL/TLS with authority 'myurl:8443'. The request was aborted: Could not create SSL/TLS secure channel.

1 个答案:

答案 0 :(得分:0)

我使用了以下代码

        var privCert = new X509Certificate2(@"c:\temp\test.p12", "testpass");

        // privCert.Verify() => FALSE!  WHY???
        if (privCert.Verify())  Console.WriteLine("true");
        else                    Console.WriteLine("false");

                    // create Web Request and set CLient Certificate für the authentication
        HttpWebRequest request = CreateWebRequest();
        request.ClientCertificates.Add(privCert);

        // Create SOAP Message for PING Command
        XmlDocument soapEnvelopeXml = new XmlDocument();
        soapEnvelopeXml.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?>
                                <soap:Envelope xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"" xmlns:ws=""http://ws.soap.transfer.services.sedna.ser.com/"">
                                    <soap:Header/>
                                    <soap:Body>
                                        <ws:ping>
                                            <sessionUUID>something</sessionUUID>
                                        </ws:ping>
                                    </soap:Body>
                                </soap:Envelope>");

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


        Console.WriteLine("Credentials of request = " + request);


        // Send Command to Webservice
        try
        {
            using (WebResponse response = request.GetResponse())
            {
                using (StreamReader rd = new StreamReader(response.GetResponseStream()))
                {
                    string soapResult = rd.ReadToEnd();
                    Console.WriteLine(soapResult);
                }
            }
        }
        catch (Exception e)
        {

            Console.WriteLine(e.Message);
        }
        Console.WriteLine("\nTaste!");
        Console.ReadKey();
    }

    static HttpWebRequest CreateWebRequest()
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"https://doxapd01.zit.commerzbank.com:8443/soapWebService");
        webRequest.Headers.Add(@"SOAP:Action");
        webRequest.ContentType = "text/xml;charset=\"utf-8\"";
        webRequest.Accept = "text/xml";
        webRequest.Method = "POST";
        return webRequest;
    }