调用webservice通过HTTP【实验】

前言

本文尝试使用HTTP请求实现调用webservice服务,需求效果参考SOAPUI。

调用WebService方法有多种:

1. 使用Visual Studio的”添加服务引用”功能,在代码中声明服务对象后,即可调用。

优点
方便快捷,节约时间
缺点:
前提条件是开发环境需要能够访问到服务地址,否则无法添加服务引用。
不适用频繁更新的webservice,每次调用的webservice的WSDL文件更新后,必须重新生成覆盖更新旧的代理类。不够便捷及时。

参考:

添加 Web 引用
访问 Web 服务

2. 使用WSDL.exe工具生成代理类,将生成的类添加到项目中,在代码中创建类的实例对象后,即可调用。

优点
方便快捷,只需要WSDL文件就能生成代理
缺点:
不适用频繁更新的webservice,每次调用的webservice的WSDL文件更新后,必须重新生成覆盖更新旧的代理类。不够便捷及时。

生成代理类
1
2
C:\Users\user>cd C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools
C:\Users\user>wsdl.exe /l:cs /n:testnamespace /out: D:testproxy.cs https://www.crcind.com/csp/samples/SOAP.Demo.CLS?WSDL=1

参考:

Web 服务描述语言工具 (Wsdl.exe)
.net wsdl.exe 生成java开发的webservice客户端类的时候报错,无法从命名空间获取绑定
C# 利用VS自带的WSDL工具生成WebService服务类

3. 使用HTTP请求实现调用webservice服务

思路

参考:

C#进阶系列——WebApi 接口返回值不困惑:返回值类型详解 - 懒得安分 - 博客园
WebService 之 WSDL文件 讲解 - 梦想的追求 - CSDN博客
WebAPI 和 webservice的区别 - CYSONG168的专栏 - CSDN博客
【WCF】什么是WCF - 白靖 - CSDN博客
mvc接口、webapi、webservice 对比 - 思明 - 博客园
使用 System.Net 和 SOAP 动态调用 Web 服务
WebService中的WSDL详细解析_孤独地搬砖的博客-CSDN博客_wsdl是什么意思
WSDL Tutorial: Web Services Description Language with Example
Web Service Definition Language (WSDL)
Web Services Description Language (WSDL) Version 2.0 Part 1: Core Language

Client发送SOAP请求并接收响应

SOAPHelper.cs
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Xml;

namespace Ryadel.Web.SOAP
{
/// <summary>
/// Helper class to send custom SOAP requests.
/// </summary>
public static class SOAPHelper
{
/// <summary>
/// Sends a custom sync SOAP request to given URL and receive a request
/// </summary>
/// <param name="url">The WebService endpoint URL</param>
/// <param name="action">The WebService action name</param>
/// <param name="parameters">A dictionary containing the parameters in a key-value fashion</param>
/// <param name="soapAction">The SOAPAction value, as specified in the Web Service's WSDL (or NULL to use the url parameter)</param>
/// <param name="useSOAP12">Set this to TRUE to use the SOAP v1.2 protocol, FALSE to use the SOAP v1.1 (default)</param>
/// <returns>A string containing the raw Web Service response</returns>
public static string SendSOAPRequest(string url, string action, Dictionary<string, string> parameters, string soapAction = null, bool useSOAP12 = false)
{
// Create the SOAP envelope
XmlDocument soapEnvelopeXml = new XmlDocument();
var xmlStr = (useSOAP12)
? @"<?xml version=""1.0"" encoding=""utf-8""?>
<soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope"">
<soap12:Body>
<{0} xmlns=""{1}"">{2}</{0}>
</soap12:Body>
</soap12:Envelope>"
: @"<?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>
<{0} xmlns=""{1}"">{2}</{0}>
</soap:Body>
</soap:Envelope>";
string parms = string.Join(string.Empty, parameters.Select(kv => String.Format("<{0}>{1}</{0}>", kv.Key, kv.Value)).ToArray());
var s = String.Format(xmlStr, action, new Uri(url).GetLeftPart(UriPartial.Authority) + "/", parms);
soapEnvelopeXml.LoadXml(s);

// Create the web request
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("SOAPAction", soapAction ?? url);
webRequest.ContentType = (useSOAP12) ? "application/soap+xml;charset=\"utf-8\"" : "text/xml;charset=\"utf-8\"";
webRequest.Accept = (useSOAP12) ? "application/soap+xml" : "text/xml";
webRequest.Method = "POST";

// Insert SOAP envelope
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}

// Send request and retrieve result
string result;
using (WebResponse response = webRequest.GetResponse())
{
using (StreamReader rd = new StreamReader(response.GetResponseStream()))
{
result = rd.ReadToEnd();
}
}
return result;
}
}
}

参考引用:

Client to send SOAP request and receive response
How to perform a SOAP Web Service Request in ASP.NET C# without using WSDL, proxy classes or SoapClient

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×