C#动态调用Web服务的3种方法

C#调用WebService的方法 上面使用静态引用的方式调用WebService,但是这种方式有一个缺点:如果发布的WebService地址改变,那么就要重新添加WebService的引用。在日常工作中,如果涉及到与第三方进行接口对接,有的会使用WebService的方式,这篇文章主要讲解在.NET Framework中如何调用WebService。1.我们在配置文件里面添加配置,把WebService的地址、WebService提供的类名、要调用的方法名称,都写在配置文件里面。这种方式是通过添加静态引用的方式调用WebService。 阅读详情

我们在开发C# WinForm时,有时会调用Web服务,服务是本地的当前好办,只要在Project中的Web References中引入就可以在代码中直接创建一个Web服务对象来引用,其实其原理是C#帮你自动创建客户端代理类的方式调用WebService,但如果调用的服务是动态的,比如说在几个IIS中都有相同的一个服务,在运行时输入具体的IP才确定调用哪个服务,那要怎么样实现呢。

C#动态调用Web服务方法一: 手动的添加一个Web引用,然后修改下本地的代理类。最后实现Web Service的URI部署到配置文件里。 具体做法如下:

以下代码是显示如何配置动态的Web Service,以服务单元C(类名为Web_SVSGC)为例:

(1)首先在Web引用中的本地代理类中添加一个构造函数,这个构造函数是以Web Service的URL为参数的重载方法。

复制  保存

   
  1. Namespace Web_SVSGC  
  2.     '< remarks/> 
  3.     < System.Diagnostics.DebuggerStepThroughAttribute(),  _     System.ComponentModel.DesignerCategoryAttribute("code"),  _     System.Web.Services.WebServiceBindingAttribute(Name:="SVSGCSoap", [Namespace]:="http://tempuri.org/QYJSERVICE/SVSGC"),  _     System.Xml.Serialization.XmlIncludeAttribute(GetType(Attribute))>  _  
  4.     Public Class SVSGC  
  5.         Inherits System.Web.Services.Protocols.SoapHttpClientProtocol  
  6.     '< remarks/> 
  7.         Public Sub New()  
  8.             MyBase.New  
  9.             Me.Url = "http://localhost/QYJSERVICE/WEBSERVICE/SERVICE/SVSGC.asmx" 
  10.         End Sub  
  11.  
  12.         '添加一个带参数的构造函数。  
  13.         Public Sub New(ByVal strUrl As String)   
  14.             MyBase.New()   
  15.             Me.Url = strUrl   
  16.         End Sub  
  17.  

(2)将Web Service的url配置在调用Web Service的应用程序的配置文件中。(其中的value可以随时修改。)

复制  保存

   
  1. < configuration> 
  2.     < appSettings> 
  3.               < add key="SVSGA_URL" value="http://192.168.108.188/ QDN/SERVICE/SVSGA.asmx" QDN/SERVICE/SVSGA.asmx" /> 
  4.     < /appSettings> 
  5. < /configuration>< configuration> 
  6.     < appSettings> 
  7.               < add key="SVSGA_URL" value="http://192.168.108.188/ QDN/SERVICE/SVSGA.asmx" QDN/SERVICE/SVSGA.asmx" /> 
  8.     < /appSettings> 
  9. < /configuration> 

(3)调用时,根据配置文件的Url动态的生成Web Service。

复制  保存       

   
  1. '要调用的Web Service的URL  
  2.         Dim strWebSvsUrl As String  
  3.         '声明一个要调用的Web Service  
  4.         Dim objSVSGC As WebSvs_GC. SVSGC  
  5.         '调用Web Service的远程方法的返回值  
  6.         Dim strReturnValue As String  
  7.         Try  
  8.             '从配置文件中取得Web Service的URL  
  9.             strWebSvsUrl = _   
  10.             System.Configuration.ConfigurationSettings.AppSettings("SVSGC_URL")   
  11.             '生成一个Web Service实例  
  12.             objSVSGC = New WebSvs_GC.SVSGC (strWebSvsUrl)  
  13.             '调用这个Web Service里的远程方法  
  14.             strReturnValue = objSVSGC.HelloWorld()  
  15.         Catch ex As Exception  
  16.         End Try 

C#动态调用Web服务方法二:完全动态处理,传入服务服务网址,方法名和参数即可.

   
  1. using System;   
  2. using System.Net;   
  3. using System.IO;   
  4. using System.CodeDom;   
  5. using Microsoft.CSharp;   
  6. using System.CodeDom.Compiler;   
  7. using System.Web.Services.Description;   
  8. using System.Web.Services.Protocols;   
  9.  
  10. namespace HB.Common   
  11. {   
  12.     /* 调用方式   
  13.      *   string url = "http://www.webservicex.net/globalweather.asmx" ;   
  14.      *   string[] args = new string[2] ;   
  15.      *   args[0] = "Hangzhou";   
  16.      *   args[1] = "China" ;   
  17.      *   object result = WebServiceHelper.InvokeWebService(url ,"GetWeather" ,args) ;   
  18.      *   Response.Write(result.ToString());   
  19.      */   
  20.     public class WebServiceHelper   
  21.     {   
  22.         #region InvokeWebService   
  23.         /// < summary>   
  24.         /// 动态调用web服务   
  25.         /// < /summary>   
  26.         /// < param name="url">WSDL服务地址< /param>   
  27.         /// < param name="methodname">方法名< /param>   
  28.         /// < param name="args">参数< /param>   
  29.         /// < returns>< /returns>   
  30.         public static object InvokeWebService(string url, string methodname, object[] args)   
  31.         {   
  32.             return WebServiceHelper.InvokeWebService(url, null, methodname, args);   
  33.         }   
  34.  
  35.         /// < summary>   
  36.         /// 动态调用web服务   
  37.         /// < /summary>   
  38.         /// < param name="url">WSDL服务地址< /param>   
  39.         /// < param name="classname">类名< /param>   
  40.         /// < param name="methodname">方法名< /param>   
  41.         /// < param name="args">参数< /param>   
  42.         /// < returns>< /returns>   
  43.         public static object InvokeWebService(string url, string classname, string methodname, object[] args)   
  44.         {   
  45.             string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling";   
  46.             if ((classname == null) || (classname == ""))   
  47.             {   
  48.                 classname = WebServiceHelper.GetWsClassName(url);   
  49.             }   
  50.  
  51.             try   
  52.             {   
  53.                 //获取WSDL   
  54.                 WebClient wc = new WebClient();   
  55.                 Stream stream = wc.OpenRead(url + "?WSDL");   
  56.                 ServiceDescription sd = ServiceDescription.Read(stream);   
  57.                 ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();   
  58.                 sdi.AddServiceDescription(sd, "", "");   
  59.                 CodeNamespace cn = new CodeNamespace(@namespace);   
  60.  
  61.                 //生成客户端代理类代码   
  62.                 CodeCompileUnit ccu = new CodeCompileUnit();   
  63.                 ccu.Namespaces.Add(cn);   
  64.                 sdi.Import(cn, ccu);   
  65.                 CSharpCodeProvider icc = new CSharpCodeProvider();   
  66.  
  67.                 //设定编译参数   
  68.                 CompilerParameters cplist = new CompilerParameters();   
  69.                 cplist.GenerateExecutable = false;   
  70.                 cplist.GenerateInMemory = true;   
  71.                 cplist.ReferencedAssemblies.Add("System.dll");   
  72.                 cplist.ReferencedAssemblies.Add("System.XML.dll");   
  73.                 cplist.ReferencedAssemblies.Add("System.Web.Services.dll");   
  74.                 cplist.ReferencedAssemblies.Add("System.Data.dll");   
  75.  
  76.                 //编译代理类   
  77.                 CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);   
  78.                 if (true == cr.Errors.HasErrors)   
  79.                 {   
  80.                     System.Text.StringBuilder sb = new System.Text.StringBuilder();   
  81.                     foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)   
  82.                     {   
  83.                         sb.Append(ce.ToString());   
  84.                         sb.Append(System.Environment.NewLine);   
  85.                     }   
  86.                     throw new Exception(sb.ToString());   
  87.                 }   
  88.  
  89.                 //生成代理实例,并调用方法   
  90.                 System.Reflection.Assembly assembly = cr.CompiledAssembly;   
  91.                 Type t = assembly.GetType(@namespace + "." + classname, true, true);   
  92.                 object obj = Activator.CreateInstance(t);   
  93.                 System.Reflection.MethodInfo mi = t.GetMethod(methodname);   
  94.  
  95.                 return mi.Invoke(obj, args);   
  96.  
  97.                 /*   
  98.                 PropertyInfo propertyInfo = type.GetProperty(propertyname);   
  99.                 return propertyInfo.GetValue(obj, null);   
  100.                 */   
  101.             }   
  102.             catch (Exception ex)   
  103.             {   
  104.                 throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));   
  105.             }   
  106.         }   
  107.  
  108.         private static string GetWsClassName(string wsUrl)   
  109.         {   
  110.             string[] parts = wsUrl.Split('/');   
  111.             string[] pps = parts[parts.Length - 1].Split('.');   
  112.  
  113.             return pps[0];   
  114.         }   
  115.         #endregion   
  116.     }   
  117. } 

返回时如果不是字符串,即强制转换,如返回是DataSet,则

   
  1. string url = "http://www.webservicex.net/globalweather.asmx" ;   
  2. string[] args = new string[2] ;   
  3. args[0] = "Hangzhou";   
  4. args[1] = "China" ;   
  5. object result = WebServiceHelper.InvokeWebService(url ,"GetWeather" ,args) ;   
  6. DataSet DSRe=(DataSet)result;  

C#动态调用Web服务方法三:URL Behavior 属性

如果知道服务的方法和参数,只是调用的URL网址会随时变化,那么可以手工创建一个服务,添加上对应的的方法和传入参数,然后引入到项目中,就可以直接开发,在创建服务的实例化时,才修改对应的URL即可.

例如服务中有个方法叫GetTax,那么就可以这样改:

   
  1. GetTax.GetTax GetTax1 = new GetTax.GetTax();   
  2. GetTax1.Url = "http://" + WebIp1 + "/pub_wa_gspsp1/gettax.asmx";        //动态引入服务器   
  3.                    
  4. DataSet DS1 = GetTax1.GetTaxMx(Bm1, OldBz, Fpl, SLx, StaDa, EndDa);   //调用服务器返回开票数据 
C# 动态调用WebService C#调用WebService的方式有很多: 1、直接在项目中添加WebService引用,这种方式比较简单,但不是动态的,即每次服务地址或者内容变了之后都要重新添加引用。 2、用Soap,这种方式需要把添加WebService引用生成的Reference.cs类中服务接口,集成进自己定义的服务调用类中,如果服务 接口发生了改变,需要修改服务调用类。 3、动态调用,代码如下: 阅读详情

相关推荐

C#_.NET_动态调用webservice的三种方式

C#_.NET_动态调用webservice的三种方式

C# 动态获取webservice的接口

【代码】C# 动态获取webservice的接口。

ilikehigame的专栏 1113

C#动态webservice调用接口

C#动态webservice调用接口 using System; using System.Collections; using System.IO; using System.Net; using System.Text; using System.Xml; using System.Xml.Serialization; namespace Hishop.Plugins { /// /// 利用WebRequest/WebResponse进行WebService调用的类 /// public class WebServiceCall

C#动态调用webServices(详细)

首先我们要创建好webServices项目文件可以是一个项目也可以是一个单独的文件放在其他项目中。 创建webServices文件的头部,引用依赖将AJAX调用此web服务取消注释。 /// <summary> /// AddApplyOnline 的摘要说明 /// </summary> [WebService(Namespace = "http://192.168.1.48:3030/AddApplyOnline.asmx/AddApplyOnlin

Y2210151431的博客 4656

tree_sitter多语言解析器配置实战指南

本文是一份详细的Tree-sitter多语言解析器配置实战指南。文章从零开始,手把手指导如何搭建Python环境、安装指定版本的tree-sitter库、下载并编译多种语言的语法规则,最终构建一个统一的解析器库。通过实战代码演示了如何初始化解析器、解析代码并遍历语法树,旨在帮助开发者高效处理多语言代码分析任务,并分享了高级技巧与常见问题排坑经验。

weixin_29245767的博客 267

Blender关于GIS安装及使用天地图

Blender下载安装及GIS插件的安装及使用

Miss_SQ的博客 6971

C#如何动态调用Web服务

  我们在开发C# WinForm时,有时会调用Web服务,服务是本地的当前好办,只要在Project中的Web References中引入就可以在代码中直接创建一个Web服务对象来引用,其实其原理是C#帮你自动创建客户端代理类的方式调用WebService,但如果调用的服务是动态的,比如说在几个IIS中都有相同的一个服务,在运行时输入具体的IP才确定调用哪个服务,那要怎么样实现呢。方法一

同一天空下 5167

AI智能体丨使用Coze一键生成爆款书单视频,3个月内获赞131.1w!(附保姆级教程)

这篇教程详细介绍了如何使用扣子(Coze)平台的AI智能工作流批量制作爆款书单视频。通过自动化流程,只需1-2分钟即可生成45-50秒的书单视频,效率提升近百倍。工作流包含文案生成、音频合成、图片素材获取、视频剪辑等全流程,支持随机生成8本书单、自动提取金句、智能配音等功能。教程提供了完整的操作步骤,包括剪映草稿配置、特效添加等细节,并附体验链接和示例效果。最终生成的视频包含书单展示、金句字幕、背景音乐等元素,适合书单号运营者快速批量产出内容。

李同学Lino的博客 2354

C# Winform动态调用Web Service

写项目的时候,忽然遇到一个小问题,在Winform里需要调用Web Service(以前都是调用WCF的,封装好的DLL就可以,比较简单) 上网了解了一下,找到一位师兄写的 http://www.csharpwin.com/csharpspace/10261r1810.shtml(这个地址不知道是转帖还是原创),他采用了动态编译的方式。 我把代码套上试验了一下,能用是肯定的,但是貌似Win

成长的步伐 5523

RK3588 buildroot 制作的系统增加 docker 支持

RK3588 使用buildroot制作的系统添加 docker 和 docker-compose 的功能

荻夜的博客 1567

25、BDS-3卫星信号性能与PPP-B2b增强消息完整性分析

本文对BDS-3卫星信号性能及PPP-B2b增强消息的完整性进行了系统分析。评估了MEO、IGSO和GEO卫星的轨道与时钟精度,得出BDS-3总体SISRE为0.25 m,系统完整性可覆盖99.999%的误差。分析表明SIS中断主要由时钟调整和轨道机动引起,个别卫星存在因境外监测受限导致的非计划中断。通过自研解码软件对PPP-B2b增强消息进行采集与解析,实验结果显示BDS-3和GPS的平均历元完整性率均高于98.55%,其中卫星掩码和时钟校正消息的完整性率超过99.8%。研究还发现IGSO卫星相比MEO卫

quiet的专栏 145

C#动态调用web服务 远程调用技术WebService

一、课程介绍 一位伟大的讲师曾经说过一句话:事物存在即合理!意思就是说:任何存在的事物都有其存在的原因,存在的一切事物都可以找到其存在的理由,我们应当把焦点放在因果关联的本质上。所以在本次分享课开课之前,我们要“约法三章”不谈论以下几个比较“严肃”和“敏感”的话题:WebService已经过时了啦,学习它干什么用啊!为什么要用WebService,而不用基于当前流行的RestFul ASP.NET WebAPI ? 对于上面的问题阿笨的回答很简单:因为它就是它,不一样烟火的WebService! 本.

cao919的专栏Net 2499

Python爬虫实战:利用Selenium与Playwright抓取美团外卖数据

在当今大数据时代,网络数据采集已成为商业分析和市场研究的重要手段。本文以美团外卖平台为例,详细介绍如何使用Python最新爬虫技术(Selenium、Playwright)进行数据采集,并探讨相关技术原理、反爬应对策略及数据存储方案。通过5000+字的详细讲解和完整代码示例,帮助读者掌握现代网络爬虫开发的核心技能。本文详细介绍了使用Python最新爬虫技术采集美团外卖数据的完整流程,涵盖了从环境配置、页面分析到数据存储的全链路解决方案。

2201_76125261的博客 2550

C# 调用WebService的3种方式 :直接调用、根据wsdl生成webservice的.cs文件及生成dll调用、动态调用...

  1.直接调用 已知webservice路径,则可以直接 添加服务引用--高级--添加web引用 直接输入webservice URL。这个比较常见也很简单 即有完整的webservice文件目录如下图所示, 也可以在本地IIS根据webservice文件目录新发布一个webservice,然后程序动态调用,修改Url public new string Url { set; ...

weixin_33849215的博客 5145

170、电机控制中的温升测试

本文以一次IGBT炸管事故为切入点,深入剖析电机控制中温升测试的实质:不仅是温度测量,更是对铜损、铁损、散热效率等热管理问题的系统验证。作者强调测试前热电偶布置、环境记录、预运行的重要性,提出实时监控电流谐波、绕组电阻、散热梯度等关键参数,并通过损耗分离与冷却系统实测,揭示真实热源。结合温升曲线异常分析与经验建议,强调留余量、仿真结合实测、关注瞬态工况等实践要点,警示温升测试是保障可靠性与安全性的核心环节。

2401_88858637的博客 48

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

C#中Winform动态调用Webservice的方法

一般情况下winform调用webservice时步骤 1添加服务引用---高级----添加web引用------填写url--添加web引用即可完成对webservice的引用 让VS.NET环境来为我们生成服务代理,然后调用对应的Web服务。 如果需要动态调用WebService,要实现这样的功能: publicstaticobjectInvokeWebService(stringur

xinxin_cheer的专栏 4789

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain&#39;s apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath(&#39;DeepLearnToolbox&#39;));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand(&#39;state&#39;,0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W&#39;); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand(&#39;state&#39;,0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = &#39;sigm&#39;;%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, &#39;Too big error&#39;);```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand(&#39;state&#39;,0)sae = saesetup([784 100]);sae.ae{1}.activation_function = &#39;sigm&#39;;sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)&#39;)% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;;nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, &#39;Too big error&#39;);```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x&#39;,28,28,60000))/255;test_x = double(reshape(test_x&#39;,28,28,10000))/255;train_y = double(train_y&#39;);test_y = double(test_y&#39;);%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you&#39;ll get around 1.2% errorrand(&#39;state&#39;,0)cnn.layers = { struct(&#39;type&#39;, &#39;i&#39;) %input layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 6, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %sub sampling layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 12, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, &#39;Too big error&#39;);```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, &#39;Too big error&#39;);%% ex2 neural net with L2 weight decayrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex3 neural net with dropoutrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex4 neural net with sigmoid activation functionrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex5 plotting functionalityrand(&#39;state&#39;,0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = &#39;softmax&#39;; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand(&#39;state&#39;,0)nn = nnsetup([784 20 10]); nn.output = &#39;softmax&#39;; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

C#编程,调用(静态、动态) Web Service 的一种方法。

  WebService 的调用分为静态调用和动态调用两种。 静态:根据写死的服务地址调用服务。 动态:根据传入的不同地址调用对应的服务。 1、静态调用 静态调用的方式是通过“Add Service Reference...”创建客户端代理类。 这种方式让VS.NET环境来为我们生成服务代理,然后调用对应的Web服务。 这样是使工作简单了,但是却将提供Web服务的URL、方法名、...

君一席 2994

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

上一篇: 使用SqlDependency时提示当前数据库未启用Service broker
下一篇: js 常用类
andycode
博客等级 码龄17年 12粉丝 80原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值