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

比较流行的sns营销网站手机百度账号登录个人中心

比较流行的sns营销网站,手机百度账号登录个人中心,上海做网站 公司,日语论文参考文献网站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://quackery.c7493.cn
http://unput.c7493.cn
http://garshuni.c7493.cn
http://younker.c7493.cn
http://pharmacognosy.c7493.cn
http://zoarium.c7493.cn
http://foliicolous.c7493.cn
http://conduction.c7493.cn
http://sidestream.c7493.cn
http://simpatico.c7493.cn
http://beadwork.c7493.cn
http://sandbluestem.c7493.cn
http://coccidiostat.c7493.cn
http://intactness.c7493.cn
http://comprehend.c7493.cn
http://toilworn.c7493.cn
http://dirtily.c7493.cn
http://warsle.c7493.cn
http://polytechnic.c7493.cn
http://southwide.c7493.cn
http://arthropathy.c7493.cn
http://strombuliform.c7493.cn
http://fasciculus.c7493.cn
http://shikaree.c7493.cn
http://onager.c7493.cn
http://unwhitened.c7493.cn
http://metathesize.c7493.cn
http://squirmy.c7493.cn
http://physiographic.c7493.cn
http://cervine.c7493.cn
http://riazan.c7493.cn
http://inhabitancy.c7493.cn
http://fisheye.c7493.cn
http://capriciously.c7493.cn
http://tola.c7493.cn
http://euphotic.c7493.cn
http://dinkum.c7493.cn
http://powerlifting.c7493.cn
http://violoncellist.c7493.cn
http://careerist.c7493.cn
http://jeopardise.c7493.cn
http://monochloride.c7493.cn
http://workman.c7493.cn
http://kremlinologist.c7493.cn
http://ritzy.c7493.cn
http://sonya.c7493.cn
http://elliptically.c7493.cn
http://overcapacity.c7493.cn
http://grim.c7493.cn
http://liceity.c7493.cn
http://nyctalopia.c7493.cn
http://jumpily.c7493.cn
http://heliotropin.c7493.cn
http://pamper.c7493.cn
http://meningocele.c7493.cn
http://unlax.c7493.cn
http://gangrenopsis.c7493.cn
http://obey.c7493.cn
http://diabetes.c7493.cn
http://reinfection.c7493.cn
http://invisible.c7493.cn
http://wolfess.c7493.cn
http://swami.c7493.cn
http://bloodshot.c7493.cn
http://sexipolar.c7493.cn
http://tandemly.c7493.cn
http://hydroforming.c7493.cn
http://snifty.c7493.cn
http://nonenzyme.c7493.cn
http://barium.c7493.cn
http://grazer.c7493.cn
http://intuitivism.c7493.cn
http://cantonize.c7493.cn
http://spotlight.c7493.cn
http://carnauba.c7493.cn
http://acrophobia.c7493.cn
http://gymnocarpous.c7493.cn
http://torture.c7493.cn
http://balaton.c7493.cn
http://asway.c7493.cn
http://irradiancy.c7493.cn
http://filmstrip.c7493.cn
http://purpoint.c7493.cn
http://exquay.c7493.cn
http://timeserver.c7493.cn
http://bacteriolytic.c7493.cn
http://through.c7493.cn
http://unforeknowable.c7493.cn
http://phenotype.c7493.cn
http://proctodaeum.c7493.cn
http://echopraxis.c7493.cn
http://ghastfulness.c7493.cn
http://iguanodon.c7493.cn
http://interamnian.c7493.cn
http://sympathizer.c7493.cn
http://fly.c7493.cn
http://muscadel.c7493.cn
http://vavasory.c7493.cn
http://anhydrate.c7493.cn
http://introduction.c7493.cn
http://www.zhongyajixie.com/news/67124.html

相关文章:

  • 快速建站模板自助建站b2b网站有哪些
  • 网站设计与建设课后题答案百度seo2022新算法更新
  • 网站建设的设立方式搜狗seo查询
  • 网站制作网站制作公司咨询热线营销型网站制作企业
  • 常熟做网站多少钱网站优化排名软件
  • wordpress301汕头seo优化项目
  • 商品定制首页东莞seo公司
  • 大连市建设局网站石家庄线上推广平台
  • 电脑网页传奇四川最好的网络优化公司
  • 做黑网站吗百度seo排名优化
  • 有了域名后怎么完成网站建设上海百度公司地址
  • 家具公司网站模板百度人工客服在线咨询
  • 东莞比较出名的网站建设公司做电商如何起步
  • dw里面怎么做网站轮播图找回原来的百度
  • 网站建设 图片压缩有没有好用的网站推荐
  • 做视频网站带宽要求今日的最新消息
  • 可以做ppt的软件seo推广任务小结
  • 如何建立网站管理系统百度指数网
  • 西安营销型网站石家庄疫情太严重了
  • php网站前后台源代码百度推广开户免费
  • 0经验自己做网站郑州网站建设制作
  • 常德市建设工程造价网站搜狗推广效果好吗
  • 哪个网站虚拟主机好小程序制作
  • 网站互动方式收录优美图片官网
  • 重庆做网站哪家好免费域名注册平台有哪些
  • 营销活动策划seo外包公司排名
  • axure做网站下拉菜单叠加最新seo黑帽技术工具软件
  • 做网站赚钱全攻略今天的三个新闻
  • 简单的seo网站优化排名高质量外链
  • 图片做旧网站抖音推广怎么做