C#调用webservice接口的最新方法教程
作者:上青天揽月 时间:2022-12-22 05:07:27
前言
Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术。是:通过SOAP在Web上提供的软件服务,使用WSDL文件进行说明,并通过UDDI进行注册。
XML:(Extensible Markup Language)扩展型可标记语言。面向短期的临时数据处理、面向万维网络,是Soap的基础。
Soap:(Simple Object Access Protocol)简单对象存取协议。是XML Web Service 的通信协议。当用户通过UDDI找到你的WSDL描述文档后,他通过可以SOAP调用你建立的Web服务中的一个或多个操作。SOAP是XML文档形式的调用方法的规范,它可以支持不同的底层接口,像HTTP(S)或者SMTP。
WSDL:(Web Services Description Language) WSDL 文件是一个 XML 文档,用于说明一组 SOAP 消息以及如何交换这些消息。大多数情况下由软件自动生成和使用。
UDDI (Universal Description, Discovery, and Integration) 是一个主要针对Web服务供应商和使用者的新项目。在用户能够调用Web服务之前,必须确定这个服务内包含哪些商务方法,找到被调用的接口定义,还要在服务端来编制软件,UDDI是一种根据描述文档来引导系统查找相应服务的机制。UDDI利用SOAP消息机制(标准的XML/HTTP)来发布,编辑,浏览以及查找注册信息。它采用XML格式来封装各种不同类型的数据,并且发送到注册中心或者由注册中心来返回需要的数据。
C#调用webservice接口
我们在日常开发中,经常遇到C#调用webservice的情况,通常来说如果webservice是用vs+c#来开发的,问题一般来说不大,直接web引用,然后调用就OK了。
流程如下:
下面就是进行调用,就这么简单。
但如果webservice是用JAVA或者其它语言或者其它工具生成的话,使用vs+c#来调用就经常遇到问题;就是使用上面的方法显得很不好使,经常是使用SOAP UI调用没有问题,但使用上面的方法却调用报错,经常是500的错误。当你联系webservice提供商时通常会说SOAP UI都能调用得到,你们用代码为啥子调用不到,问题出在你们的调用方法上。
在我们向其它公司提供webservice的时候,经常也会出现这样的问题,以前我们一直都以为SOAP UI能够调用,那么代码也就一定能够调用得通,但经过实践,我们自己写DEMO调用自己的webservice时才发现,并不是别人的调用代码写的有问题,因为我们自己也无法将自己写的webservice调用得通,或者说没有找到正确的方法调用得通。
这时我们就要思考是否是SOAP UI能够调用得通的webservice就代码一定调用没有问题呢?或者说SOAP UI调用webservice和代码调用webservice的原理区别到底在哪里呢?
总结一下:
(1)SOAP UI能够调用成功,代码不一定能够调用成功,代码调用成功并且得到返回结果的前提是webservice可以按标准返回结果,但SOAP UI是只要按信封返回就可以收到结果而不管结果是否标准;
(2)如果webservice的header有用户名和密码的校验,使用SOAP UI可以调用成功并且得到返回结果,但使用上面web引用的方式却不行。对于这种情况,有以下方法可以调用成功:
重复上面web引用的方式,另外引用Microsoft.Web.Services3(这个DLL可以在这里下载)。
手工修改Reference.cs
以下是调用方法,重点在传入用户名和密码,修改继承类的重点也在于此;
引用完成,调用就会成功了,结果如下:
从上面可以看出web引用真的是简单粗暴,基本不用写几句代码就可以搞定绝大部分的webservice调用。
但是web引用却有在现实开发中常遇到的缺点:
(1)需要开发环境可以访问到的wsdl地址;
(2)如果webservice有变化需要更新web引用;
那有没有其它方法呢?答案是有的。一种是直接封装信封,使用WebRequest,代码如下:
private void button2_Click(object sender, EventArgs e)
{
StringBuilder soap = new StringBuilder();
soap.Append("<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:zls=\"www.zlsoft.cn\">");
soap.Append("<soap:Header>");
soap.Append("<wsse:Security soap:mustUnderstand=\"true\" xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">");
soap.Append("<wsse:UsernameToken wsu:Id=\"UsernameToken-4\">");
soap.Append("<wsse:Username>hjq</wsse:Username>");//用户名
soap.Append("<wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">123</wsse:Password>");//口令
soap.Append("<wsse:Nonce EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\">gC6dDGKjxo0IaRc5KpQU3w==</wsse:Nonce>");
soap.Append("<wsu:Created>2017-11-01T05:11:22.805Z</wsu:Created>");
soap.Append("</wsse:UsernameToken>");
soap.Append("</wsse:Security>");
soap.Append("</soap:Header>");
soap.Append("<soap:Body>");
soap.Append("<zls:getUserInfo>");
soap.Append("<zls:appsys_id>trwetre</zls:appsys_id>");
soap.Append("<zls:appsys_token>sdafdsf</zls:appsys_token>");
soap.Append("<zls:sid>fdsafds</zls:sid>");
soap.Append("</zls:getUserInfo>");
soap.Append("</soap:Body>");
soap.Append("</soap:Envelope>");
string url = "http://127.0.0.1:6998/services/HIPService?wsdl";
var result = GetSOAPReSource(url, soap.ToString());
}
public static string GetSOAPReSource(string url, string datastr)
{
try
{
//request
Uri uri = new Uri(url);
WebRequest webRequest = WebRequest.Create(uri);
webRequest.ContentType = "application/soap+xml; charset=utf-8";
webRequest.Method = "POST";
using (Stream requestStream = webRequest.GetRequestStream())
{
byte[] paramBytes = Encoding.UTF8.GetBytes(datastr.ToString());
requestStream.Write(paramBytes, 0, paramBytes.Length);
}
//response
WebResponse webResponse = webRequest.GetResponse();
using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
{
string result = "";
return result = myStreamReader.ReadToEnd();
}
}
catch (Exception ex)
{
throw ex;
}
}
但是上述方式还是存在问题,如果webservice需要校验header里面的用户名与密码时,虽然信封中封装了用户名和密码,但是一样调用不成功,同样的信封内容,使用SOAP UI却能成功。可见代码调用和SOAP UI调用还是有一定区别的。
另外一种方式是使用动态编译:
Uri uri = new Uri(txt_url.Text);
WebRequest webRequest = WebRequest.Create(uri);
webRequest.Credentials = new NetworkCredential("hjq", "123");
System.IO.Stream requestStream = webRequest.GetResponse().GetResponseStream();
// Get a WSDL file describing a service
ServiceDescription sd = ServiceDescription.Read(requestStream);
string sdName = sd.Services[0].Name;
// Add in tree view
// Initialize a service description servImport
ServiceDescriptionImporter servImport = new ServiceDescriptionImporter();
servImport.AddServiceDescription(sd, String.Empty, String.Empty);
servImport.ProtocolName = "Soap";
servImport.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties;
CodeNamespace nameSpace = new CodeNamespace();
CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
codeCompileUnit.Namespaces.Add(nameSpace);
// Set Warnings
ServiceDescriptionImportWarnings warnings = servImport.Import(nameSpace, codeCompileUnit);
if (warnings == 0)
{
StringWriter stringWriter = new StringWriter(System.Globalization.CultureInfo.CurrentCulture);
Microsoft.CSharp.CSharpCodeProvider prov = new Microsoft.CSharp.CSharpCodeProvider();
prov.GenerateCodeFromNamespace(nameSpace, stringWriter, new CodeGeneratorOptions());
// Compile the assembly with the appropriate references
//string[] assemblyReferences = new string[2] { "System.Web.Services.dll", "System.Xml.dll" };
string[] assemblyReferences = new string[3] {"System.dll", "System.dll", "System.dll" };
CompilerParameters param = new CompilerParameters(assemblyReferences);
param.GenerateExecutable = false;
param.GenerateInMemory = true;
param.TreatWarningsAsErrors = false;
param.WarningLevel = 4;
CompilerResults results = new CompilerResults(new TempFileCollection());
results = prov.CompileAssemblyFromDom(param, codeCompileUnit);
Assembly assembly = results.CompiledAssembly;
Type service = assembly.GetType(sdName);
MethodInfo[] methodInfo = service.GetMethods();
foreach (MethodInfo t in methodInfo)
{
if (t.Name == "Discover")
break;
if (t.Name == cbMethods.Text)
{
//MessageBox.Show(t.ToString());
//Invoke Method
Object obj = Activator.CreateInstance(service);
Object response = t.Invoke(obj, new object[] { "1","2","3"});
MessageBox.Show("Result = " + response.ToString());
break;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
从这种方式可以看出,首先是访问wsdl的地址取回webservice的结构,然后采取动态编译生成本地程序集的方式去调用webservice,和web引用自动生成Reference.cs的原理类似;但是明显这种方式也存在问题,如果webservice的header中需要校验用户名与密码怎么处理呢?
来源:http://www.cnblogs.com/goldenbridge/p/7781482.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
一步步教你写一个SpringMVC框架
![](https://img.aspxhome.com/file/2023/9/93139_0s.jpg)
Android实现纸飞机的简单操作
MyBatis一对多嵌套查询的完整实例
浅谈Springboot下引入mybatis遇到的坑点
![](https://img.aspxhome.com/file/2023/1/72551_0s.png)
SpringBoot读写操作yml配置文件方法
![](https://img.aspxhome.com/file/2023/8/99918_0s.png)
Java通过JavaMail发送邮件功能
![](https://img.aspxhome.com/file/2023/1/122291_0s.jpg)
Android实现随机圆形云标签效果
![](https://img.aspxhome.com/file/2023/1/137651_0s.png)
浅析C++中的函数与指针
![](https://img.aspxhome.com/file/2023/6/125906_0s.png)
Java里的static import使用小结
spring cloud升级到spring boot 2.x/Finchley.RELEASE遇到的坑
![](https://img.aspxhome.com/file/2023/6/90026_0s.jpg)
Qt之调用C#的动态库的解决方法
![](https://img.aspxhome.com/file/2023/1/67061_0s.png)
c#之利用API函数实现动画窗体的方法详解
Android Studio利用AChartEngine制作饼图的方法
![](https://img.aspxhome.com/file/2023/0/139460_0s.png)
Android仿淘宝物流信息TimeLineView
![](https://img.aspxhome.com/file/2023/6/111706_0s.jpg)
Android自定义视图中图片的处理
![](https://img.aspxhome.com/file/2023/6/139576_0s.png)
Java中的OkHttp使用教程
简单掌握Android Widget桌面小部件的创建步骤
实例详解用户输入 i. 检测常用手势
spring中bean id相同引发故障的分析与解决
![](https://img.aspxhome.com/file/2023/2/61872_0s.jpg)
spring cloud整合ribbon问题及解决方案
![](https://img.aspxhome.com/file/2023/3/57823_0s.png)