WebClient调用WebService
(文末下载完整代码)
先上代码:
object[] inObjects = new[] { "14630, 14631" }; HttpWebClient wc = new HttpWebClient(2300); var result1 = WebServiceClientHelper.InvokeWebService("ESBService_TEST", "http://localhost/ESBService/VitalSign.svc?wsdl", "QueryVocabSet", inObjects, wc); WriteLine(result1.ToString());
public class HttpWebClient : WebClient { /// <summary> /// 初始化需要设置超时时间,以毫秒为单位 /// </summary> /// <param name="timeout">毫秒为单位</param> public HttpWebClient(int timeout) { Timeout = timeout; } public int Timeout { get; set; } /// <summary> /// 重写 GetWebRequest,添加 WebRequest 对象超时时间 /// </summary> protected override WebRequest GetWebRequest(Uri address) { HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address); request.Timeout = Timeout; request.ReadWriteTimeout = Timeout; return request; } }
HttpWebRequest 改造依据:
WebClient |
HttpWebRequest |
提供用于将数据发送到由 URI 标识的资源及从这样的资源接收数据的常用方法。 |
public class HttpWebRequest : WebRequest, ISerializable |
Assembly location: C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.dll |
Assembly location: C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.dll |
protected virtual WebRequest GetWebRequest(Uri address) { WebRequest request = WebRequest.Create(address); this.CopyHeadersTo(request); if (this.Credentials != null) request.Credentials = this.Credentials; if (this.m_Method != null) request.Method = this.m_Method; if (this.m_ContentLength != -1L) request.ContentLength = this.m_ContentLength; if (this.m_ProxySet) request.Proxy = this.m_Proxy; if (this.m_CachePolicy != null) request.CachePolicy = this.m_CachePolicy; return request; } protected virtual WebResponse GetWebResponse(WebRequest request) { WebResponse response = request.GetResponse(); this.m_WebResponse = response; return response; }
|
public class HttpWebClient : WebClient { /// <summary> /// 初始化需要设置超时时间,以毫秒为单位 /// </summary> /// <param name="timeout">毫秒为单位</param> public HttpWebClient(int timeout) { Timeout = timeout; } public int Timeout { get; set; } /// <summary> /// 重写 GetWebRequest,添加 WebRequest 对象超时时间 /// </summary> protected override WebRequest GetWebRequest(Uri address) { HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address); request.Timeout = Timeout; request.ReadWriteTimeout = Timeout; return request; } }
|
调用服务处理:
public static object InvokeWebService(string providerName, string url, string methodName, object[] args, WebClient wc = null) { object result = null; if (wc == null) wc = new WebClient(); using (wc) { using (Stream wsdl = wc.OpenRead(url)) { var client = GetClient(wsdl, url, methodName, providerName); client.SetValue("Timeout", wsdl.ReadTimeout); result = client.InvokeService(args); } } return result; }
形如这样 http://192.168.2.100:8090/services/dududuTest?wsdl 的地址,
返回的是 dududuTest 服务下公开的方法,以流的形式,
代码处理里面需要解读这种流,目前看到的一种方式是,把这个流解读编译成一个动态的dll,利用反射,动态调用方法。
/// <summary>为从具有 <see cref="T:System.String" /> 指定的 URI 的资源下载的数据打开一个可读的流。</summary> /// <returns>一个 <see cref="T:System.IO.Stream" />,用于从资源读取数据。</returns> /// <param name="address">以 <see cref="T:System.String" /> 形式指定的 URI,将从中下载数据。</param> public Stream OpenRead(string address) { if (address == null) throw new ArgumentNullException(nameof (address)); return this.OpenRead(this.GetUri(address)); } /// <summary>为从具有 <see cref="T:System.Uri" /> 指定的 URI 的资源下载的数据打开一个可读的流</summary> /// <returns>一个 <see cref="T:System.IO.Stream" />,用于从资源读取数据。</returns> /// <param name="address">以 <see cref="T:System.Uri" /> 形式指定的 URI,将从中下载数据。</param> public Stream OpenRead(Uri address) { if (Logging.On) Logging.Enter(Logging.Web, (object) this, nameof (OpenRead), (object) address); if (address == (Uri) null) throw new ArgumentNullException(nameof (address)); WebRequest request = (WebRequest) null; this.ClearWebClientState(); try { request = this.m_WebRequest = this.GetWebRequest(this.GetUri(address)); Stream responseStream = (this.m_WebResponse = this.GetWebResponse(request)).GetResponseStream(); if (Logging.On) Logging.Exit(Logging.Web, (object) this, nameof (OpenRead), (object) responseStream); return responseStream; } catch (Exception ex) { Exception innerException = ex; if (innerException is ThreadAbortException || innerException is *Exception || innerException is OutOfMemoryException) { throw; } else { if (!(innerException is WebException) && !(innerException is SecurityException)) innerException = (Exception) new WebException(SR.GetString("net_webclient"), innerException); WebClient.AbortRequest(request); throw innerException; } } finally { this.CompleteWebClientState(); } }View Code
类 DefaultWebServiceClient 定义:
public class DefaultWebServiceClient { Type _type; MethodInfo _method; object _obj; public object InvokeService(object[] args) { object proxy = GetProxy(); return _method.Invoke(proxy, args); } public void SetValue(string fieldName, object value) { object proxy = GetProxy(); PropertyInfo field = _type.GetProperty(fieldName); if (field != null) field.SetValue(proxy, value); } public object GetProxy() { if (_obj == null) _obj = Activator.CreateInstance(_type); return _obj; } public MethodInfo MethodInfo { get { return _method; } } public DefaultWebServiceClient(Stream wsdl, string url, string methodname, string providerName) { if (wsdl == null || (wsdl.CanWrite && wsdl.Length == 0)) throw new Exception("Wsdl为空"); try { ServiceDescription sd = ServiceDescription.Read(wsdl); ServiceDescriptionImporter sdi = new ServiceDescriptionImporter(); sdi.AddServiceDescription(sd, "", ""); CodeNamespace cn = new CodeNamespace(string.Format("DefaultWebServiceClient_{0}_{1}", providerName, wsdl.GetHashCode().ToString())); DiscoveryClientProtocol dcp = new DiscoveryClientProtocol(); dcp.DiscoverAny(url); dcp.ResolveAll(); foreach (object osd in dcp.Documents.Values) { if (osd is ServiceDescription) sdi.AddServiceDescription((ServiceDescription)osd, null, null); ; if (osd is XmlSchema) sdi.Schemas.Add((XmlSchema)osd); } //生成客户端代理类代码 CodeCompileUnit ccu = new CodeCompileUnit(); ccu.Namespaces.Add(cn); sdi.Import(cn, ccu); CSharpCodeProvider csc = new CSharpCodeProvider(); ICodeCompiler icc = csc.CreateCompiler(); //设定编译器的参数 CompilerParameters cplist = new CompilerParameters(); cplist.GenerateExecutable = false; cplist.GenerateInMemory = true; cplist.ReferencedAssemblies.Add("System.dll"); cplist.ReferencedAssemblies.Add("System.XML.dll"); cplist.ReferencedAssemblies.Add("System.Web.Services.dll"); cplist.ReferencedAssemblies.Add("System.Data.dll"); //编译代理类 CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu); if (true == cr.Errors.HasErrors) { System.Text.StringBuilder sb = new StringBuilder(); foreach (CompilerError ce in cr.Errors) { sb.Append(ce.ToString()); sb.Append(System.Environment.NewLine); } throw new Exception(sb.ToString()); } //生成代理实例,并调用方法 Assembly assembly = cr.CompiledAssembly; Type type = null; foreach (Type t in assembly.GetExportedTypes()) { if (t.GetCustomAttributes(typeof(System.Web.Services.WebServiceBindingAttribute), false).Count() > 0) { type = t; break; } } MethodInfo mi = null; if (type == null) { throw new Exception("从Wsdl中找不到可调用的类型"); } mi = type.GetMethod(methodname); if (mi == null) { throw new Exception(string.Format("从Wsdl中找不到可调用的方法{0}.{1}", type.Name, methodname)); } _type = type; _method = mi; } catch (Exception ex) { throw new Exception("创建WebService客户端失败!", ex); } } }View Code
WebClient 是一个操作 WebRequest 的类型,相当于一个 worker,它把服务器能提供的内容(资源)都以流的形式返回来,供我们调用,
解读这种流,需要 ServiceDescription 类实例,再将每项加载入 ServiceDescriptionImporter 类的实例中,
再把它编译成能本地使用的组件,动态地调用,相当于运行时加载了一个dll。
(注:三种方法调用 webService https://www.jb51.net/article/190211.htm)
|
验证 1 |
验证 2 |
|
新建了一个webClient(服务代理), 它请求的处理对象默认是webRequest, 因为webRequest没有TimeOut属性,于是我继承了webCLient,override处理对象为HttpWebRequest (注,HttpWebRequest 是 WebRequest的子类) |
新建了一个webClient, 它请求的处理对象默认是webRequest
|
结果 |
在完成整个调用当中charles获取了2次http请求,其中: 第一次:在获取wsdl流的时候,请求包是http请求的get方式; 第二次:加载动态dll反射调用方法时,请求包是http请求的post方式; |
同左 |
证 |
webClient真的是一个代理,像一个worker,代码上看似在本地动态引用了第三方的dll,但实际上还是http请求 |
就算不写死类型为 HttpWebRequest,识别出来的对象仍然是HttpWebRequest 的实例,这个服务本质提供的就是 http 请求 |
待证 |
|
识别为http请求应该是url的前缀是http的原因,如果是ftp://...那么识别出来就应该是ftpWebRequest |
这种 WebClient 的方式应该只适用于取 wsdl 来处理,
我尝试直接用 HttpWebRequest 用 get 方法取 http://localhost/ESBService/VitalSign.svc?wsdl 的内容,返回了一段服务中的方法说明 xml,
而我试过直接照着第二次包的请求内容来发 HttpWebRequest,并不能成功(报错500内部服务器出错),可能是我入参处理不对。
WebClient 方式和直接发 HttpWebRequest 方式应该是互通的,可以互相转换的,
不过 WebClient 使用 Httpwebrequest 的方式封装了别的处理,某些程度上减少了程序员的工作,总归是数据结构整合的问题,这块就不多研究了。
WebClient封装 HttpWebRequest 的部分内容(头部等),它还提供了一些方法,
这里有一个示例,是异步请求,得到响应后触发事件的适用,不过这个实例比较久远了,2008年的:
https://docs.microsoft.com/zh-cn/archive/blogs/silverlight_sdk/using-webclient-and-httpwebrequest
.Net当中,WebRequest 与 HttpWebRequest 区别,从名字感觉很相似,反编译结果确实是。
由客服端构建的(不同协议的)请求发给服务器 | ||||
WebRequest |
FtpWebRequest |
FileWebRequest |
HttpWebRequest |
WebSocket |
抽象基类 |
继承于WebRequest |
继承于WebRequest |
继承于WebRequest |
抽象基类 |
与WebResponse成对存在 |
ftp:// 开头 |
file:// 开头,一般是打开本机文件, 形如: file:///C:/Users/dududu/0209.pdf file://dududu/Share2021/ |
http:// 开头 |
ws:// 开头 wss:// |
|
|
|
Websocket主要是javaScript在页面中使用,不需要像http请求那样等待一整个页面文件返回,只取业务数据。 |
放一些地址给观察观察,感知请求和接受的运作模式,各有性格,殊途同归:
http://192.168.8.100:9000/empId=<EmployeeId>&jobId=<JobId>
get 方式,相当于有这样一个格式的服务地址,可以返回一个超文本内容(网页)或者一些某种格式的数据。
(我比较好奇是服务器怎么响应网络,光知道配置总显得苍白)
http://172.18.99.100:8080/yyzx/index.action?userName=<userName>&passWord=<passWord>
有些网址中这样是等价的,应该是哪里(服务配置、架构配置等)处理到了这个逻辑:
http://192.168.29.100:8081/emr/index.php/index/emr/getemr?personid=7321446
http://192.168.29.100:8081/emr/index.php/index/emr/getemr/personid/7321446
附:点击下载完整代码