无需任何库,从任何编程语言使用 TCP/IP 调用 Webservice






4.82/5 (10投票s)
如何从任何编程语言调用任何 Web 服务
引言
在本文档中,我想解释如何从任何编程语言(C、C++、C#、Java 等)调用任何 Web 服务。
通过本文,您还可以使用支持 TCP/IP 协议的掌上电脑、移动设备或任何终端来调用 Web 服务。
众所周知,我们可以在 C# 中通过向项目中添加 Web 引用并调用 Web 服务的函数来调用 Web 服务。
如果您想知道如何操作,可以在 Google 上搜索,您会找到很多描述如何执行此操作的页面。
对于需要使用 C 语言连接到 Web 服务的程序员,他们可以使用任何 SOAP 库来调用 Web 服务。对我来说,我使用了 GSOAP
库。 它足以用于此方法。
但在这里,我们不想添加 Web 引用,也不想使用任何外部库。
我们想使用以下三个要素来调用 Web 服务:服务器 IP、服务器端口、包含我们请求的数据以及用于接收响应的数据。
1-服务器 IP
我们的服务器是拥有 Web 服务的服务器。在我的示例中,我使用了一个免费的 Web 服务 http://www.webservicex.net/globalweather.asmx,所以我的服务器是 http://www.webservicex.net/,其 IP 地址是 173.201.44.188。
2-服务器端口
现在,我将选择哪个端口来发送我的请求? 实际上,您在这里只有一个选择。
因为我们的调用将是 HTTP POST,正如我们将要看到的那样,我们将使用默认的 HTTP 端口来调用任何 Web 服务。 此端口是 80。
3- 数据
我们想要确定的最后一件事是我们要发送的数据。 幸运的是,它掌握在我们手中。 打开我的 Web 服务示例 http://www.webservicex.net/globalweather.asmx 并选择 GetCitiesByCountry
函数。
这将是为您打开的页面 http://www.webservicex.net/globalweather.asmx?op=GetCitiesByCountry。 您现在可以看到一个可以输入您想要获取其城市的国家/地区的地方,并且页面中还有一些 HTTP 请求/响应代码。
我们将采用 SOAP 1.1 下的 HTTP 请求。
POST /globalweather.asmx HTTP/1.1
Host: www.webservicex.net
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.webserviceX.NET/GetCitiesByCountry"
<?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>
<GetCitiesByCountry xmlns="http://www.webserviceX.NET">
<CountryName>string</CountryName>
</GetCitiesByCountry>
</soap:Body>
</soap:Envelope>
这将是我们发送到服务器的 XML 请求。
在 XML 请求的开头,我们可以看到 HTTP 方法是 POST
,并确定了调用的一些参数,在 Body
中,它确定了我想调用的函数。
在此代码中,我们将把单词字符串替换为我们要获取其城市的国家/地区,例如埃及,我们将把长度替换为该请求有多少字节? (数数吧 :-p)
将此数据发送到服务器后,我们将获得如下所示的 XML 响应
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?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>
<GetCitiesByCountryResponse xmlns="http://www.webserviceX.NET">
<GetCitiesByCountryResult>string</GetCitiesByCountryResult>
</GetCitiesByCountryResponse>
</soap:Body>
</soap:Envelope>
但是用从 Web 服务返回的所有响应替换 string
,具体取决于我们调用的函数。
现在,从任何可以使用 TCP/IP 协议发送和接收数据的终端,您都有 IP、端口、数据。 将数据发送到 IP 和端口,您就可以收到响应。
代码
在这里,我使用 C# 为我的话语做了一个简单的测试,我使用它是因为它很容易理解,但你可以使用任何其他语言。
首先,我们使用 System.Net
和 System.Net.Sockets
来使用 TCP 函数。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
我使用了 System.IO
,因为我将把我的请求和响应保存在文件中,所以我希望对文件进行读写操作。
我创建了一个 TCP 客户端,并将其连接到服务器 IP 和端口 80,正如我之前所说的那样
TcpClient myclient = new TcpClient();//create a client will not be
//myclient.Connect("173.201.44.188", 80);
我的请求位于一个名为 data.txt 的 txt 文件中。 我打开它并从它填充请求到字节数组。 你可以使用任何你想要的方法用请求填充这个数组。 只需用请求填充它。
FileStream data = new FileStream("data.txt", FileMode.Open); //open the file
//that has the xml data
int i;
for ( i = 0; i < data.Length; i++)
{
mydata[i] = (byte)data.ReadByte();//fill my array with the xml string
}
data.Close();//close the file
然后我发送了我的数据并等待了一段时间
Stream clientstream = myclient.GetStream();
clientstream.Write(mydata, 0, 1000);//send my xml data
label1.Text = "Data Sent....";
Thread.Sleep(4000);//wait for some time
然后我收到了结果并将其写入 textbox
和另一个名为 response.txt 的文件中
data = new FileStream("response.txt", FileMode.Create);//create another file
//to see the response
StreamReader sr = new StreamReader(clientstream); textBox1.Text=sr.ReadToEnd();//read the
//response
data.Write(Encoding.ASCII.GetBytes(textBox1.Text), 0,
Encoding.ASCII.GetByteCount(textBox1.Text));
data.Close();
data.txt 的内容是
POST /globalweather.asmx HTTP/1.1
Host: www.webservicex.net
Content-Type: text/xml; charset=utf-8
Content-Length: 571
SOAPAction: "http://www.webserviceX.NET/GetCitiesByCountry"
<?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>
<GetCitiesByCountry xmlns="http://www.webserviceX.NET">
<CountryName>Egypt</CountryName>
</GetCitiesByCountry>
</soap:Body>
</soap:Envelope>
response.txt 的内容将是
HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Content-Length: 1728
Content-Type: text/xml; charset=utf-8
Server: Microsoft-IIS/7.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 09 Jan 2012 13:01:45 GMT
<?xml version="1.0" encoding="utf-8"?><soap:Envelope
xmlns:soap=http://schemas.xmlsoap.org/soap/envelope/
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body>
<GetCitiesByCountryResponse xmlns="http://www.webserviceX.NET">
<GetCitiesByCountryResult><NewDataSet>
<Table>
<Country>Egypt</Country>
<City>El Arish</City>
</Table>
<Table>
<Country>Egypt</Country>
<City>Asyut</City>
</Table>
<Table>
<Country>Egypt</Country>
<City>Alexandria / Nouzha</City>
</Table>
<Table>
<Country>Egypt</Country>
<City>Cairo Airport</City>
</Table>
<Table>
<Country>Egypt</Country>
<City>Hurguada</City>
</Table>
<Table>
<Country>Egypt</Country>
<City>Luxor</City>
</Table>
<Table>
<Country>Egypt</Country>
<City>Mersa Matruh</City>
</Table>
<Table>
<Country>Egypt</Country>
<City>Port Said</City>
</Table>
<Table>
<Country>Egypt</Country>
<City>Sharm El Sheikhintl</City>
</Table>
<Table>
<Country>Egypt</Country>
<City>Asswan</City>
</Table>
<Table>
<Country>Egypt</Country>
<City>El Tor</City>
</Table>
</NewDataSet></GetCitiesByCountryResult></GetCitiesByCountryResponse>
</soap:Body></soap:Envelope>HTTP/1.1 400 Bad Request
Content-Type: text/html; charset=us-ascii
Server: Microsoft-HTTPAPI/2.0
Date: Mon, 09 Jan 2012 13:01:45 GMT
Connection: close
Content-Length: 326
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid Verb</h2>
<hr><p>HTTP Error 400. The request verb is invalid.</p>
</BODY></HTML>
此函数将返回埃及的所有城市作为数据集。