先为啥要纯手工打造呢,因为对方是用C++做的,我按照他们给出的WSDL实现了一个WebService,结果他们完全不能调用。具体是他们调用的问题还是WSDL定义的问题,不可考了。
悲催的弱势方……只能我们去配合他们。不提了。
首先用C#调用对方的WebService。
因为对方用的C++实现,添加Web服务引用方式无效……直接添加WSDL的调用对方不认……
只好手工打造,还好C#足够强大。
HttpWebRequest类可以简单的实现WebService调用。
首先手工打造SOAP包内容
string soap =
"<soapenv:Envelope xmlns:soapenv=/"http://schemas.xmlsoap.org/soap/envelope//" xmlns:down=/"http://down.wsdl.position.mdd.ailk.com/" xmlns:in=/"http://in.object.down.wsdl.position.mdd.ailk.com/">"
+ "<soapenv:Header/>"
+ "<soapenv:Body>"
+ "<TestFunc>"
+ "<object>Test Message</object>"
+ "</TestFunc>"
+ "</soapenv:Body>"
+ "</soapenv:Envelope>";
构造HttpWebRequest对象
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost:8088/");
request.Method = "POST";
request.ContentType = "text/xml; charset=utf-8";
request.Headers.Add("SOAPAction", "/TestFunc");
byte[] bts = Encoding.UTF8.GetBytes(soap);
发送请求
request.ContentLength = data.Length;
Stream writer = request.GetRequestStream();
writer.Write(data, 0, data.Length);
writer.Close();
获取返回信息
StreamReader sr = new StreamReader(request.GetResponse().GetResponseStream(), Encoding.UTF8);
String retXml = sr.ReadToEnd();
sr.Close();
之后按照WSDL的定义,解析返回的XML串就好了。
当然手工打造就是累……
如果是标准的WebService,直接一句string str = Serv.TestFunc(“”);
不提了。
C#实现WebService给对方调用
按照我的理解,WebService底层是SOAP,本质上就是一个TCP的短连接。
解决的思路就是用C#来实现一个TCP的服务端,然后手工分析请求内容。
//开始监听端口
myListener = new TcpListener(IPAddress.Parse(ListenIpAddr), ListenPort);
myListener.Start();
接收连接,并且获取请求内容
//接受新连接
Socket mySocket = myListener.AcceptSocket();
string sBuffer = "";
Byte[] bReceive = new Byte[2048];
// 接收请求内容
int i = mySocket.Receive(bReceive, bReceive.Length, 0);
sBuffer = Encoding.ASCII.GetString(bReceive);
// 只处理 "POST"请求类型
if (sBuffer.Substring(0, 4) != "POST")
{
return;
}
截取soap:Body部分
string soap = "<?xml version=/"1.0/" encoding=/"utf-8/" ?>/n";
int iStartPos = sBuffer.IndexOf("<soap:Body>", 1);
int iStopPos = sBuffer.IndexOf("</soap:Body>", 1);
if (iStartPos > 0)
{
soap = soap + sBuffer.Substring(iStartPos, iStopPos - iStartPos + 9);
}
分析请求内容这里就略了。
返回消息的构造如下。
首先构造消息体内容
string ret = string.Format(
""
+ "<soapenv:Envelope xmlns:soapenv=/"http://schemas.xmlsoap.org/soap/envelope//" xmlns:down=/"http://down.wsdl.position.mdd.ailk.com/" xmlns:out=/"http://out.object.down.wsdl.position.mdd.ailk.com/">/n"
+ "<soapenv:Header/>/n"
+ "<soapenv:Body>/n"
+ "<TestRsp>/n"
+ "<object ResultCode=/"0/"/>/n"
+ "</TestRsp>/n"
+ "</soapenv:Body>/n"
+ "</soapenv:Envelope>"
);
byte[] bytes = Encoding.ASCII.GetBytes(ret);
构造消息头部 // 构造头部
String sMimeType = "text/html";
string sHttpVersion = "HTTP/1.1 ";
String sBuffer = "";
if (sMIMEHeader.Length == 0)
{
sMIMEHeader = "text/html"; // 默认 text/html
}
sBuffer = sBuffer + sHttpVersion + " 200 OK" + "/r/n";
sBuffer = sBuffer + "Server: cx1193719-b/r/n";
sBuffer = sBuffer + "Content-Type: " + sMimeType + "/r/n";
sBuffer = sBuffer + "Accept-Ranges: bytes/r/n";
sBuffer = sBuffer + "Content-Length: " + iTotBytes + "/r/n/r/n";
Byte[] heads = Encoding.ASCII.GetBytes(sBuffer);
然后发送给对方,关闭Socket。mySocket.Send(heads, heads.Length, 0);
mySocket.Send(bytes, bytes.Length, 0);
mySocket.Close();