栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用SOAP调用WCF服务

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用SOAP调用WCF服务

@John
Saunders的评论是正确的。无论您使用ASMX做什么,都应该能够使用WCF。实际上,只要您执行适当的SOAP请求,Web服务使用哪种框架/技术都没有关系。

WCF只是一个有助于构建面向服务的应用程序的框架。像任何其他此类框架一样,它使您能够专注于将要提供的实际服务,并照顾了将该服务公开为SOAP
Web服务所需的所有管道功能。

至于SoapUI,它是一个Java工具,可让您测试Web服务。当您向它提供WSDL时,它会动态创建请求样本,然后使用Http
Client(如果我没记错的话)将其发送到Web服务。

如果您有WCF Web服务,那么什么都不会发生。即使使用这样的基本客户端,仍然可以进行SOAP通信:

public class Program{    public static void Main(string[] args)    {        // OK, this is not a WCF web service, but that should not matter :D        string endpoint = "http://www.html2xml.nl/Services/Calculator/Version1/Calculator.asmx";        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);        request.ContentType = "text/xml"; // or application/soap+xml for SOAP 1.2        request.Method = "POST";        request.KeepAlive = false;        //In case you have a proxy to resolve the server name also add these lines        var proxyServer = new WebProxy("XX.XX.XX.XX", 1234);        proxyServer.Credentials = CredentialCache.DefaultCredentials; // or username + password        request.Proxy = proxyServer;        // you can read these from files        string payload = @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:tem=""http://tempuri.org/"">         <soapenv:Header/>         <soapenv:Body> <tem:Add>    <tem:a>1</tem:a>    <tem:b>2</tem:b> </tem:Add>         </soapenv:Body>      </soapenv:Envelope>";        byte[] byteArray = Encoding.UTF8.GetBytes(payload);        request.ContentLength = byteArray.Length;        Stream requestStream = request.GetRequestStream();        requestStream.Write(byteArray, 0, byteArray.Length);        requestStream.Close();        HttpWebResponse response = null;        try        {  response = (HttpWebResponse)request.GetResponse();        }        catch (WebException ex)        {  response = (HttpWebResponse)ex.Response;        }        Console.WriteLine(string.Format("HTTP/{0} {1} {2}n", response.ProtocolVersion, (int)response.StatusCode, response.StatusDescription));        // you can write this to files        Stream responseStream = response.GetResponseStream();        StreamReader reader = new StreamReader(responseStream);        Console.WriteLine(reader.ReadToEnd());        // cleanp        reader.Close();        requestStream.Close();        responseStream.Close();        response.Close();    }}

您将获得一个SOAP响应,在这种情况下,它是:

HTTP/1.1 200 OK<?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>        <AddResponse xmlns="http://tempuri.org/"> <AddResult>3</AddResult>        </AddResponse>    </soap:Body></soap:Envelope>

不管是生成它的是ASMX,还是WCF等等。这是对HTTP请求的响应。

相反,如果您发送无效消息,例如:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">   <soapenv:Header/>   <soapenv:Body>      <tem:Add>         <tem:a>x</tem:a>         <tem:b>y</tem:b>      </tem:Add>   </soapenv:Body></soapenv:Envelope>

您会找回故障,例如:

HTTP/1.1 500 Internal Server Error<?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>        <soap:Fault> <faultpre>soap:Client</faultpre> <faultstring> ... exception stacktrace here ... </faultstring> <detail />        </soap:Fault>    </soap:Body></soap:Envelope>

您可以使用SoapUI自动化测试,甚至可以将它们与Junit集成,甚至可以使用JMeter之类的东西,尽管它不是专门为Web服务而设计的(例如SoapUI),但它可以测试SOAP。您当然可以使用我添加到答案中的那个基本客户端。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/370753.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号