在没有服务引用的情况下在Visual Studio 2012中调用SOAP服务

时间:2017-11-09 22:38:29

标签: c# xml visual-studio-2012 soap soap-client

当我添加服务引用时,代理生成存在缺陷,所以我必须下载使用XML /构建soap信封等。

用于此目的的最佳课程是什么?

目前我正在使用WebClient类尝试发送带有soap信封的HTTP请求作为有效负载和soap操作头等,但还有其他我不知道的类吗?例如创建肥皂信封包装的类?用于创建SoapClient的类?

private string SendServiceCall(string action, XElement requestData, string url)
        {
            //load our cert


                string response = "";
                //MyWebClient : System.Net.WebClient
                using (MyWebClient client = new MyWebClient(signingCertificate)) {

                    client.Headers.Add("SOAPAction", @"http://tempuri.org/HelloWorld");
                    client.Headers.Add("Content-Type", "text/xml; charset=utf-8");

                    string payload = @"<?xml version=""1.0"" encoding=""utf-8""?><soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><soap:Body>" + requestData.ToString() + "</soap:Body></soap:Envelope>";

                    //log request
                    System.IO.File.AppendAllText("c:/temp/request.xml", payload);
                    byte[] data = Encoding.UTF8.GetBytes(payload);



                    //parse response
                    try {
                        byte[] result = client.UploadData(url, data);
                        response = Encoding.Default.GetString(result);
                        System.IO.File.AppendAllText(@"c:/temp/response.xml", payload);
                    } catch (Exception ex) {

                        Log(String.Format("Send Service Call Failed Action: {0} URL: {1} Message: {2}",action,url,ex.Message));

                    }


                }



            return "";


        }

2 个答案:

答案 0 :(得分:0)

除了WCF之外,没有用于SOAP服务的客户端或库。 如果您不想在VS中通过服务引用添加wsdl,那么您应该尝试使用SOAP标头而不是WebClient来使用HttpWebRequest。 因为WebClient是较高级别的HttpWebRequest而且速度较慢。

答案 1 :(得分:0)

这样做的结果是让服务提供者给我他们的接口类,因为WSDL没有正确生成代理。然后我通过channelfactory运行界面,如下所示:

ChannelFactory<ICourtRecordMDEPort> factory = new ChannelFactory<ICourtRecordMDEPort>("epConfig");
ICourtRecordMDEPort client = factory.CreateChannel();
CreateCaseResponse resp = client.CreateCase(req);
factory.Close();

“epConfig”是web / app.config中已配置端点的名称属性。

相关问题