当前位置: 首页 > news >正文

比较流行的sns营销网站1000个关键词

比较流行的sns营销网站,1000个关键词,南宁做网约车哪个平台比较好,网站建设怎么做c# WebService创建与调用 文章目录 c# WebService创建与调用一、前言二、webService相关术语1.XML2. WSDL3. SOAP 三、创建四、调用4.1 添加引用的方式4.2 动态代理的方式4.3 HttpWebRequest的方式 五、扩展 一、前言 WebService,顾名思义就是基于Web的服务。它使用…

c# WebService创建与调用


文章目录

  • c# WebService创建与调用
    • 一、前言
    • 二、webService相关术语
      • 1.XML
      • 2. WSDL
      • 3. SOAP
    • 三、创建
    • 四、调用
      • 4.1 添加引用的方式
      • 4.2 动态代理的方式
      • 4.3 HttpWebRequest的方式
    • 五、扩展


一、前言

WebService,顾名思义就是基于Web的服务。它使用Web(HTTP)方式,接收和响应外部系统的某种请求。从而实现远程调用。

我们可以调用互联网上查询天气信息Web服务,然后将它嵌入到我们的程序(C/S或B/S程序)当中来,当用户从我们的网点看到天气信息时,他会认为我们为他提供了很多的信息服务,但其实我们什么也没有做,只是简单调用了一下服务器上的一段代码而已。

ISO的七层模型 : 物理层、数据链路层、网络层、传输层、表示层、会话层、应用层

Socket访问 : Socket属于传输层,它是对Tcp/ip协议的实现,包含TCP/UDP,它是所有通信协议的基础,Http协议需要Socket支持,以Socket作为基础

Socket通信特点:

  • 开启端口,该通信是 长连接的通信 ,很容易被防火墙拦截,可以通过心跳机制来实现 ,开发难度大 传输的数据一般是字符串 ,可读性不强
  • socket端口不便于推广
  • 性能相对于其他的通信协议是最优的

Http协议访问 : 属于应用层的协议,对Socket进行了封装

  • 跨平台
  • 传数据不够友好
  • 对第三方应用提供的服务,希望对外暴露服务接口问题
  • 数据封装不够友好 :可以用xml封装数据
  • 希望给第三方应用提供web方式的服务 (http + xml) = web Service

二、webService相关术语

1.XML

Extensible Markup Language -扩展性标记语言

  • XML,用于传输格式化的数据,是Web服务的基础。
  • namespace-命名空间。
  • xmlns=“http://itcast.cn” 使用默认命名空间。
  • xmlns:itcast=“http://itcast.cn”使用指定名称的命名空间。

2. WSDL

WebService Description Language – Web服务描述语言。

  • 通过XML形式说明服务在什么地方-地址。
  • 通过XML形式说明服务提供什么样的方法 – 如何调用。

3. SOAP

Simple Object Access Protocol –简单对象访问协议

  • SOAP作为一个基于XML语言的协议用于有网上传输数据。
  • SOAP = 在HTTP的基础上+XML数据。
  • SOAP是基于HTTP的。

SOAP的组成如下:

  • Envelope – 必须的部分。以XML的根元素出现。
  • Headers – 可选的。
  • Body – 必须的。在body部分,包含要执行的服务器的方法。和发送到服务器的数据。

三、创建

新建项目,选择 “ASP.NET Web应用程序(.NET Framework)”。

在这里插入图片描述

填写好项目名称、选择项目位置以及所使用的框架,然后点击创建。

在这里插入图片描述
选择一个空模板,去掉为https配置选项,然后创建。

在这里插入图片描述

打开解决方案资源管理器-右键创建的web项目-添加-新建项-添加 web 服务(AMSX)

在这里插入图片描述
默认的是HelloWorld方法,自己可以添加几个方法。

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;namespace webServiceServer
{/// <summary>/// WebService1 的摘要说明/// </summary>[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][System.ComponentModel.ToolboxItem(false)]// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 // [System.Web.Script.Services.ScriptService]public class WebService1 : System.Web.Services.WebService{[WebMethod]public string HelloWorld(){return "Hello World";}[WebMethod(Description = "求和的方法")]public double addition(double i, double j){return i + j;}[WebMethod(Description = "求差的方法")]public double subtract(double i, double j){return i - j;}[WebMethod(Description = "求积的方法")]public double multiplication(double i, double j){return i * j;}[WebMethod(Description = "求商的方法")]public double division(double i, double j){if (j != 0)return i / j;elsereturn 0;}}
}

启动项目。在上面我们可以看到我们所写的五个方法,我选择其中一个点进去,点击调用后我们可以看到输出了“4”。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
到这里,就创建好了一个服务了。

四、调用

4.1 添加引用的方式

新建项目-添加-服务引用,打开刚刚启动的网站,复制这个地址粘贴到服务引用中。

在这里插入图片描述
在这里插入图片描述
接下来点击高级,添加Web 引用(W)-在打开的界面中的URL中输入刚刚复制的网址-点击蓝色箭头-添加引用,即可在解决方案资源管理器中看到我们所添加的服务引用。
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
这样引用就添加好了,那我们现在就开始使用吧。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

直接运行一下点击按钮,看看结果

在这里插入图片描述

调用成功~

4.2 动态代理的方式

新建一个代理类
在这里插入图片描述

using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Web.Services.Description;
using System.Xml.Serialization;namespace webServiceClient
{public class WebServiceHelper{public static void CreateWebServiceDLL(string url, string className, string methodName, string filePath){// 1. 使用 WebClient 下载 WSDL 信息。WebClient web = new WebClient();//Stream stream = web.OpenRead(url + "?WSDL");//CertificateTrust.SetCertificatePolicy();//证书出现问题时调用此代码//未能为 SSL/TLS 安全通道建立信任关系Stream stream = web.OpenRead(url);// 2. 创建和格式化 WSDL 文档。ServiceDescription description = ServiceDescription.Read(stream);//如果不存在就创建file文件夹if (Directory.Exists(filePath) == false){Directory.CreateDirectory(filePath);}if (File.Exists(filePath + className + "_" + methodName + ".dll")){//判断缓存是否过期var cachevalue = HttpRuntime.Cache.Get(className + "_" + methodName);if (cachevalue == null){//缓存过期删除dllFile.Delete(filePath + className + "_" + methodName + ".dll");}else{// 如果缓存没有过期直接返回return;}}// 3. 创建客户端代理代理类。ServiceDescriptionImporter importer = new ServiceDescriptionImporter();// 指定访问协议。importer.ProtocolName = "Soap";// 生成客户端代理。importer.Style = ServiceDescriptionImportStyle.Client;importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;// 添加 WSDL 文档。importer.AddServiceDescription(description, null, null);// 4. 使用 CodeDom 编译客户端代理类。// 为代理类添加命名空间,缺省为全局空间。CodeNamespace nmspace = new CodeNamespace();CodeCompileUnit unit = new CodeCompileUnit();unit.Namespaces.Add(nmspace);ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit);CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");CompilerParameters parameter = new CompilerParameters();parameter.GenerateExecutable = false;// 可以指定你所需的任何文件名。parameter.OutputAssembly = filePath + className + "_" + methodName + ".dll";parameter.ReferencedAssemblies.Add("System.dll");parameter.ReferencedAssemblies.Add("System.XML.dll");parameter.ReferencedAssemblies.Add("System.Web.Services.dll");parameter.ReferencedAssemblies.Add("System.Data.dll");// 生成dll文件,并会把WebService信息写入到dll里面CompilerResults result = provider.CompileAssemblyFromDom(parameter, unit);if (result.Errors.HasErrors){// 显示编译错误信息System.Text.StringBuilder sb = new StringBuilder();foreach (CompilerError ce in result.Errors){sb.Append(ce.ToString());sb.Append(System.Environment.NewLine);}throw new Exception(sb.ToString());}//记录缓存var objCache = HttpRuntime.Cache;// 缓存信息写入dll文件objCache.Insert(className + "_" + methodName, "1", null, DateTime.Now.AddMinutes(5), TimeSpan.Zero, CacheItemPriority.High, null);}}
}

调用方法,前面四个配置信息可以写在app.config里,也可以直接代码里写死。

在这里插入图片描述

using System;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using webServiceClient.myWebService;namespace webServiceClient
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){WebService1 wb = new WebService1();double s = wb.subtract(10, 6);textBox1.Text = s.ToString();}private void UseWebService(string xml){// 读取配置文件,获取配置信息string url = string.Format(@"http://localhost:52264/WebService1.asmx");//ConfigurationManager.AppSettings["WebServiceAddress"];//WebService地址string className = "WebService1";//ConfigurationManager.AppSettings["ClassName"];//WebService提供的类名string methodName = "subtract";//ConfigurationManager.AppSettings["MethodName"];// WebService方法名string filePath = string.Format(@"D:\test\webServiceServer\bin\");//ConfigurationManager.AppSettings["FilePath"];//存放dll文件的地址// 调用WebServiceHelperWebServiceHelper.CreateWebServiceDLL(url, className, methodName, filePath);// 读取dll内容byte[] filedata = File.ReadAllBytes(filePath + className + "_" + methodName + ".dll");// 加载程序集信息Assembly asm = Assembly.Load(filedata);Type t = asm.GetType(className);// 创建实例object o = Activator.CreateInstance(t);MethodInfo method = t.GetMethod(methodName);// 参数//object[] args = { xml };object[] args = { 10,2};// 调用访问,获取方法返回值string value = method.Invoke(o, args).ToString();//输出返回值MessageBox.Show($"返回值:{value}");}private void button2_Click(object sender, EventArgs e){UseWebService("111");}}
}

在这里插入图片描述

调用成功~

4.3 HttpWebRequest的方式

新建一个HttpHelper帮助类
在这里插入图片描述

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Xml;namespace webServiceClient
{public class HttpHelper{public static string CallServiceByGet(string strURL){//创建一个HTTP请求HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);request.Method = "get";HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();Stream s = response.GetResponseStream();//转化为XML,自己进行处理XmlTextReader Reader = new XmlTextReader(s);Reader.MoveToContent();string strValue = Reader.ReadInnerXml();Reader.Close();strValue = strValue.Replace("<", "<");strValue = strValue.Replace(">", ">");return strValue;}public static string CallServiceByPost(string strURL, Dictionary<string,string> parameters){//创建一个HTTP请求HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);//Post请求方式request.Method = "POST";//内容类型request.ContentType = "application/x-www-form-urlencoded";//设置参数,并进行URL编码StringBuilder codedString = new StringBuilder();foreach (string key in parameters.Keys){codedString.Append(HttpUtility.UrlEncode(key));codedString.Append("=");codedString.Append(HttpUtility.UrlEncode(parameters[key]));codedString.Append("&");}string paraUrlCoded = codedString.Length == 0 ? string.Empty : codedString.ToString().Substring(0, codedString.Length - 1);//string paraUrlCoded = HttpUtility.UrlEncode("ProductId");//paraUrlCoded += "=" + HttpUtility.UrlEncode(this.textBox1.Text);byte[] payload;//将URL编码后的字符串转化为字节payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);//设置请求的ContentLengthrequest.ContentLength = payload.Length;//发送请求,获得请求流Stream writer = request.GetRequestStream();//将请求参数写入流writer.Write(payload, 0, payload.Length);//关闭请求流writer.Close();//获得响应流HttpWebResponse response = (HttpWebResponse)request.GetResponse();Stream s = response.GetResponseStream();//转化为XML,自己进行处理XmlTextReader Reader = new XmlTextReader(s);Reader.MoveToContent();string strValue = Reader.ReadInnerXml();Reader.Close();strValue = strValue.Replace("<", "<");strValue = strValue.Replace(">", ">");return strValue;}}}

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Security.Policy;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using webServiceClient.myWebService;namespace webServiceClient
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){WebService1 wb = new WebService1();double s = wb.subtract(10, 6);textBox1.Text = s.ToString();}private void UseWebService(string xml){// 读取配置文件,获取配置信息string url = string.Format(@"http://localhost:52264/WebService1.asmx");//ConfigurationManager.AppSettings["WebServiceAddress"];//WebService地址string className = "WebService1";//ConfigurationManager.AppSettings["ClassName"];//WebService提供的类名string methodName = "subtract";//ConfigurationManager.AppSettings["MethodName"];// WebService方法名string filePath = string.Format(@"D:\test\webServiceServer\bin\");//ConfigurationManager.AppSettings["FilePath"];//存放dll文件的地址// 调用WebServiceHelperWebServiceHelper.CreateWebServiceDLL(url, className, methodName, filePath);// 读取dll内容byte[] filedata = File.ReadAllBytes(filePath + className + "_" + methodName + ".dll");// 加载程序集信息Assembly asm = Assembly.Load(filedata);Type t = asm.GetType(className);// 创建实例object o = Activator.CreateInstance(t);MethodInfo method = t.GetMethod(methodName);// 参数//object[] args = { xml };object[] args = { 10,2};// 调用访问,获取方法返回值string value = method.Invoke(o, args).ToString();//输出返回值MessageBox.Show($"返回值:{value}");}private void button2_Click(object sender, EventArgs e){UseWebService("111");}private void button3_Click(object sender, EventArgs e){Dictionary<string, string> parameters = new Dictionary<string, string>();parameters.Add("i", "5");parameters.Add("j", "2");string url = string.Format(@"http://localhost:52264/WebService1.asmx/addition");var result = HttpHelper.CallServiceByPost(url, parameters);}}
}

调用成功~

到这里,我们就把三种调用方式讲完咯。

五、扩展

这里有一些别人写好的服务,我们可以直接调用。

调用别人写好的webService,来体验一把 http://www.webxml.com.cn/zh_cn/index.aspx

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述


文章转载自:
http://entasis.zfqr.cn
http://lincoln.zfqr.cn
http://antistrophe.zfqr.cn
http://bubble.zfqr.cn
http://picaninny.zfqr.cn
http://unitable.zfqr.cn
http://confederative.zfqr.cn
http://ubiquity.zfqr.cn
http://disfrock.zfqr.cn
http://exhaustible.zfqr.cn
http://lavatorial.zfqr.cn
http://nosogenetic.zfqr.cn
http://creditably.zfqr.cn
http://thrang.zfqr.cn
http://recumbently.zfqr.cn
http://knightly.zfqr.cn
http://tipi.zfqr.cn
http://andvar.zfqr.cn
http://strode.zfqr.cn
http://improvident.zfqr.cn
http://knee.zfqr.cn
http://saxophone.zfqr.cn
http://baloney.zfqr.cn
http://croatia.zfqr.cn
http://hob.zfqr.cn
http://orangeman.zfqr.cn
http://blinker.zfqr.cn
http://eustonian.zfqr.cn
http://parthenopaeus.zfqr.cn
http://agonizing.zfqr.cn
http://teetery.zfqr.cn
http://olio.zfqr.cn
http://meaningless.zfqr.cn
http://ersatz.zfqr.cn
http://valerie.zfqr.cn
http://egger.zfqr.cn
http://barstool.zfqr.cn
http://paleontologist.zfqr.cn
http://leporine.zfqr.cn
http://rheebuck.zfqr.cn
http://barretry.zfqr.cn
http://unaccountably.zfqr.cn
http://sporades.zfqr.cn
http://antibacchii.zfqr.cn
http://teaser.zfqr.cn
http://corollary.zfqr.cn
http://disabuse.zfqr.cn
http://jejunum.zfqr.cn
http://sclerotoid.zfqr.cn
http://disfiguration.zfqr.cn
http://lacerated.zfqr.cn
http://precisian.zfqr.cn
http://baffling.zfqr.cn
http://notarikon.zfqr.cn
http://stigmatize.zfqr.cn
http://lamentoso.zfqr.cn
http://undiminishable.zfqr.cn
http://polarography.zfqr.cn
http://subtreasury.zfqr.cn
http://gormandizer.zfqr.cn
http://rhombencephalon.zfqr.cn
http://caespitose.zfqr.cn
http://thoroughbred.zfqr.cn
http://refreeze.zfqr.cn
http://keyphone.zfqr.cn
http://feculence.zfqr.cn
http://minicoy.zfqr.cn
http://tumblerful.zfqr.cn
http://microseismograph.zfqr.cn
http://exheredation.zfqr.cn
http://countersign.zfqr.cn
http://morphologist.zfqr.cn
http://mankey.zfqr.cn
http://demodulate.zfqr.cn
http://zoophilous.zfqr.cn
http://subtense.zfqr.cn
http://cornetist.zfqr.cn
http://lynchpin.zfqr.cn
http://barroque.zfqr.cn
http://illegible.zfqr.cn
http://topic.zfqr.cn
http://phenanthrene.zfqr.cn
http://tegmen.zfqr.cn
http://ecology.zfqr.cn
http://praesepe.zfqr.cn
http://bailout.zfqr.cn
http://unanalysable.zfqr.cn
http://matriclan.zfqr.cn
http://executioner.zfqr.cn
http://habanera.zfqr.cn
http://tepee.zfqr.cn
http://herbert.zfqr.cn
http://backstop.zfqr.cn
http://respecting.zfqr.cn
http://unscathed.zfqr.cn
http://seropurulent.zfqr.cn
http://coach.zfqr.cn
http://esoteric.zfqr.cn
http://devolatilize.zfqr.cn
http://shaggy.zfqr.cn
http://www.hrbkazy.com/news/84966.html

相关文章:

  • 网站管理过程关键词在线播放免费
  • 专业沈阳网站建设模板建站网页
  • qq刷网站空间推广方案怎么写模板
  • 网站制作的页面比例baud百度一下
  • 招商门户网站建设方案温州seo
  • 好看欧美视频网站模板下载 迅雷下载地址湖南企业竞价优化首选
  • 织梦模板首页修改教程seo优化排名服务
  • 国外网站策划网时代教育培训机构官网
  • 百度网盟 网站定向投放百度知道合伙人答题兼职入口
  • 网站做镜像找网络公司做推广费用
  • 百度搜索站长平台种子资源
  • 税务网站建设管理指导思想什么是软文写作
  • 网站怎么做网络推广网络营销网站设计
  • 贵港网站建设怎么开通网站平台
  • 手机在线做网站市场营销公司有哪些
  • 单页购物网站源码泉州百度网站推广
  • 公安网站源码seo教程网站
  • 网站域名登记证明二级子域名ip地址查询
  • 网站的服务有哪些引擎seo优
  • 网站建设需要哪些资料搜索引擎平台有哪些软件
  • 汕尾建设网站首页电影站的seo
  • 做期货要看哪几个网站百度投诉中心人工电话
  • 中企动力天津科技有限公司seo高端培训
  • 软件开发专业技能怎么写保定seo推广公司
  • 牙科网站建设seo快速优化
  • 网站模板做的比较好的苏州优化网站公司
  • 文化建设网站广州中小企业seo推广运营
  • 赣州做网站链接怎么做
  • 福州公司网站开发方案品牌维护
  • 福建省建设法制协会网站怎么找推广渠道