您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。

2026-03-31 09:121阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计837个文字,预计阅读时间需要4分钟。

您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。

目录+前言+一、实现思路+二、步骤+1. 使用HttpListener构建服务+2. 处理请求的数据+总结+前言+使用C#实现基于HttpListener的服务端程序,以下将详细介绍实现思路和步骤。

目录
  • 前言
  • 一、实现思路
  • 二、步骤
    • 1.使用HttpListener构建服务
    • 2.处理请求的数据
  • 总结

    前言

    使用 C#以B/S方式构建WebService服务十分简便,即是使用Asp.net在网站中添加WebService服务并使用IIS发布。但如需要在C/S程序中发布WebService服务则没有直接可用的类库。因此需要使用另外的方式实现WebService服务。

    一、实现思路

    WebService实际是使用Http并遵循SOAP协议格式进行交互。能够进行Http通讯即可实现WebService服务,只是没了现成的类库就需要自己编写解析SOAP格式数据包和组织应答包。

    二、步骤

    1.使用HttpListener构建服务

    代码如下(示例):

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; using System.Net; using System.Web; namespace LadarManufacturabilityTooling {     public class HttpServic     {         public delegate byte[] OnGetResponseDataHandle(HttpListenerPostValue Sender);         public event OnGetResponseDataHandle OnGetResponse;         private static HttpListener " + HttpServerIP.ToString() + ":" + HttpServerPort.ToString() + "/");             try             {                  schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:xsd="www.w3.org/2001/XMLSchema" xmlns:client1="LSCService.chinamobile.com" xmlns:service1="FSUService.chinamobile.com"> <SOAP-ENV:Body SOAP-ENV:encodingStyle="schemas.xmlsoap.org/soap/encoding/"> <client1:invoke> <xmlData>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;&lt;Request&gt;&lt;PK_Type&gt;&lt;Name&gt;LOGIN&lt;/Name&gt;&lt;/PK_Type&gt;&lt;Info&gt;&lt;UserName&gt;cmcc&lt;/UserName&gt;&lt;PassWord&gt;B101341CC2E4D6F5B395C7544B96A826&lt;/PassWord&gt;&lt;FSUID&gt;21202110060001&lt;/FSUID&gt;&lt;FSUIP&gt;192.168.1.253&lt;/FSUIP&gt;&lt;FSUMAC&gt;00:21:92:01:b5:9f&lt;/FSUMAC&gt;&lt;FSUVER&gt;2.0.0.15 for CMCC&lt;/FSUVER&gt;&lt;/Info&gt;&lt;/Request&gt;&#xD;&#xA; </xmlData> </client1:invoke>< /SOAP-ENV:Body> </SOAP-ENV:Envelope>

    收到的数据包原文(Sender.datas)为:

    作为示例的服务的参数名为xmlData从SOAP中截取出参数的字符串进行处理。

    由于xmlData中的内容是一串xml字符,SOAP传输时经过了转义,因此还需要转义回来。

    string xmlstr = HttpUtility.HtmlDecode(xmlOrgstr);

    处理完相应的业务,将需要回复的数据加上SOAP协议的头尾组好回复包返回。需要转义的部分记得进行符号转义。

    System.Security.SecurityElement.Escape(LOGIN_ACK)

    SOAP协议的头尾根据WebService服务函数的定义有所不同,需要自行组织。示例如下:

    /// <summary> /// 返回完整的SOAP包 /// </summary> /// <param name="XmlData">应答部分</param> /// <returns></returns> public static string GetCompleteSoapString(string XmlData) { string restr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"schemas.xmlsoap.org/soap/envelope/\"" + " xmlns:SOAP-ENC=\"schemas.xmlsoap.org/soap/encoding/\"" + " xmlns:xsi=\"www.w3.org/2001/XMLSchema-instance\"" + " xmlns:xsd=\"www.w3.org/2001/XMLSchema\"" + " xmlns:client1=\"LService.mobile.com\"" + " xmlns:service1=\"FService.mobile.com\">" + "<SOAP-ENV:Body>" + "<client1:invokeResponse><invokeReturn>"; string restrEnd = "</invokeReturn></client1:invokeResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>"; restr = restr + XmlData + restrEnd; return restr; }

    您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。

    总结

    既然C# 并未提供在C/S程序使用的WebService服务的.Net库,那么就使用HttpListener监听http请求自行解出其中的输入数据,再根据SOAP协议进行处理。以此方式实现WebService服务。

    到此这篇关于C#中C/S端实现WebService服务的文章就介绍到这了,更多相关C# C/S端 WebService 内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

    本文共计837个文字,预计阅读时间需要4分钟。

    您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。

    目录+前言+一、实现思路+二、步骤+1. 使用HttpListener构建服务+2. 处理请求的数据+总结+前言+使用C#实现基于HttpListener的服务端程序,以下将详细介绍实现思路和步骤。

    目录
    • 前言
    • 一、实现思路
    • 二、步骤
      • 1.使用HttpListener构建服务
      • 2.处理请求的数据
    • 总结

      前言

      使用 C#以B/S方式构建WebService服务十分简便,即是使用Asp.net在网站中添加WebService服务并使用IIS发布。但如需要在C/S程序中发布WebService服务则没有直接可用的类库。因此需要使用另外的方式实现WebService服务。

      一、实现思路

      WebService实际是使用Http并遵循SOAP协议格式进行交互。能够进行Http通讯即可实现WebService服务,只是没了现成的类库就需要自己编写解析SOAP格式数据包和组织应答包。

      二、步骤

      1.使用HttpListener构建服务

      代码如下(示例):

      using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; using System.Net; using System.Web; namespace LadarManufacturabilityTooling {     public class HttpServic     {         public delegate byte[] OnGetResponseDataHandle(HttpListenerPostValue Sender);         public event OnGetResponseDataHandle OnGetResponse;         private static HttpListener " + HttpServerIP.ToString() + ":" + HttpServerPort.ToString() + "/");             try             {                  schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:xsd="www.w3.org/2001/XMLSchema" xmlns:client1="LSCService.chinamobile.com" xmlns:service1="FSUService.chinamobile.com"> <SOAP-ENV:Body SOAP-ENV:encodingStyle="schemas.xmlsoap.org/soap/encoding/"> <client1:invoke> <xmlData>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;&lt;Request&gt;&lt;PK_Type&gt;&lt;Name&gt;LOGIN&lt;/Name&gt;&lt;/PK_Type&gt;&lt;Info&gt;&lt;UserName&gt;cmcc&lt;/UserName&gt;&lt;PassWord&gt;B101341CC2E4D6F5B395C7544B96A826&lt;/PassWord&gt;&lt;FSUID&gt;21202110060001&lt;/FSUID&gt;&lt;FSUIP&gt;192.168.1.253&lt;/FSUIP&gt;&lt;FSUMAC&gt;00:21:92:01:b5:9f&lt;/FSUMAC&gt;&lt;FSUVER&gt;2.0.0.15 for CMCC&lt;/FSUVER&gt;&lt;/Info&gt;&lt;/Request&gt;&#xD;&#xA; </xmlData> </client1:invoke>< /SOAP-ENV:Body> </SOAP-ENV:Envelope>

      收到的数据包原文(Sender.datas)为:

      作为示例的服务的参数名为xmlData从SOAP中截取出参数的字符串进行处理。

      由于xmlData中的内容是一串xml字符,SOAP传输时经过了转义,因此还需要转义回来。

      string xmlstr = HttpUtility.HtmlDecode(xmlOrgstr);

      处理完相应的业务,将需要回复的数据加上SOAP协议的头尾组好回复包返回。需要转义的部分记得进行符号转义。

      System.Security.SecurityElement.Escape(LOGIN_ACK)

      SOAP协议的头尾根据WebService服务函数的定义有所不同,需要自行组织。示例如下:

      /// <summary> /// 返回完整的SOAP包 /// </summary> /// <param name="XmlData">应答部分</param> /// <returns></returns> public static string GetCompleteSoapString(string XmlData) { string restr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"schemas.xmlsoap.org/soap/envelope/\"" + " xmlns:SOAP-ENC=\"schemas.xmlsoap.org/soap/encoding/\"" + " xmlns:xsi=\"www.w3.org/2001/XMLSchema-instance\"" + " xmlns:xsd=\"www.w3.org/2001/XMLSchema\"" + " xmlns:client1=\"LService.mobile.com\"" + " xmlns:service1=\"FService.mobile.com\">" + "<SOAP-ENV:Body>" + "<client1:invokeResponse><invokeReturn>"; string restrEnd = "</invokeReturn></client1:invokeResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>"; restr = restr + XmlData + restrEnd; return restr; }

      您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。

      总结

      既然C# 并未提供在C/S程序使用的WebService服务的.Net库,那么就使用HttpListener监听http请求自行解出其中的输入数据,再根据SOAP协议进行处理。以此方式实现WebService服务。

      到此这篇关于C#中C/S端实现WebService服务的文章就介绍到这了,更多相关C# C/S端 WebService 内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!