WinForm 通过服务引用 调用 远端 WebService

  WebService 服务 很多是在 web client 界面下进行调用的, 但有时候在桌面系统中也需要调用 WebService 服务, 下面即为 WinForm 调用 WebService 服务实例:

  下面即为一个 WebSerice服务的方法:

   public string HelloName(string name)

   传入一个字符串参数, 返回值为字符串值.

  Soap 1.1 示例说明:

POST /Service1.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/HelloName"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<HelloName xmlns="http://tempuri.org/">
<name>string</name>
</HelloName>
</soap:Body>
</soap:Envelope>

 在 WinForm 调用:

 在"解决方案资源管理器"中选中打开的项目点击右键: "添加引用服务", 在地址栏输入远端服务器上的 WebService 地址, 点"前往", "服务"栏中会显示服务名称, "操作"栏会显示服务方法,

定义一个命名空间, 确定即可.

 这样, 项目中会将此 web服务应用添加进来, 再在代码中添加此命名空间, 即可如本地程序集一样调用服务.

 在新添加的程序集中就有一项 以 Client 为后缀的类, 而其中就有我们所需要调用的方法.

 如下:

     WebService1Client client = new WebService1Client();

     MessageBox.Show(client.HelloName("Jacky")); // 显示: Hello, Jacky!

如上.

原文地址:https://www.cnblogs.com/csMapx/p/2181624.html