举个简单的例子说明一下我的问题:
服务器端有WebService方法如下:
[WebMethod]
public string Hello(string name)
{
return name + " Say Hello World";
}-------------------------------------------------------------------------------------------------
创建Soap消息方法:
private byte[] BuildSoap()
{
//获取Web服务的命名空间
string service_nc;
if (txtNsOfService.Text != null && txtNsOfService.Text.Length > 0)
service_nc = txtNsOfService.Text;
else
service_nc = @"http://tempuri.org"; //创建只包含EnVelop元素的XML文档
XmlDocument doc = new XmlDocument();
string xmlStr = "<?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:Envelope>";
doc.LoadXml(xmlStr);
//"xmlns:soap='http://schemas.xmlsoap.org/soap/envelope'>" //soap 1.1 //获取文档顶级Envelope元素
XmlElement envelope = doc.DocumentElement; ///////////////////////////////////////////////
//创建并添加Header元素
if (lbxHeaderBlocks.Items.Count > 0)
{
XmlElement header = doc.CreateElement("soap", "Header", "http://schemas.xmlsoap.org/soap/envelope/");
foreach (object obj in lbxHeaderBlocks.Items)
{
HeaderBlock hb = (HeaderBlock)obj;
//创建报头元素
XmlElement block = doc.CreateElement(hb.Prefix, hb.Name, hb.Namespace);
if (hb.Actor != null)
block.SetAttribute("soap:actor", hb.Actor);
if (hb.MustUnderstand)
block.SetAttribute("soap:mustUnderstand", "true");
block.InnerXml = hb.Content; //把报头条目添加到Header元素中
header.AppendChild(block);
} //把Header元素添加到Envelope元素中
envelope.AppendChild(header);
} /////////////////////////////////////////////////////
//创建并添加body元素 if (txtMethodName.Text != null && txtMethodName.Text.Length > 0)
{
XmlElement body = doc.CreateElement("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/"); //向body元素中添加表示方法的结构元素
//创建方法结构元素
XmlElement method=doc.CreateElement(txtMethodName.Text,service_nc);
//向方法结构元素中添加表示参数的元素
foreach (string p in lbxParams.Items)
{
XmlElement elem = doc.CreateElement(p.Substring(0, p.IndexOf(":")),service_nc);
elem.InnerText = p.Substring(p.IndexOf(":") + 1);
method.AppendChild(elem);
}
//把方法元素添加到Body元素中
body.AppendChild(method);
//把Body元素添加到Envelope元素中
envelope.AppendChild(body);
} //把xml文档中的内容保存到byte数组中并返回
return Encoding.UTF8.GetBytes(doc.OuterXml);
}
-------------------------------------------------------------------------------------------------
窗体加入发送Soap消息 按钮事件:
private void btnSendMessage_Click(object sender, EventArgs e)
{
Uri theURL;
HttpWebRequest httpRequest;
try
{
theURL = new Uri(txtURI.Text);
}
catch (UriFormatException ex)
{
MessageBox.Show(this, ex.Message);
txtURI.Focus();
return;
} //以指定的URL创建请求对象
httpRequest = (HttpWebRequest)WebRequest.Create(theURL); //构造HTTP POST 消息,并把SOAP消息放到它的消息主体中
try
{
//根据用户的输入设置请求对象的标题字段
//httpRequest.ContentType = "application/soap+xml; charset=utf-8";
httpRequest.ContentType = "text/xml; charset=utf-8"; //添加SOAP Action 字段
string action;
if (txtNsOfService.Text != null && txtNsOfService.Text.Length > 0)
action = txtNsOfService.Text;
else
action = "http://tempuri.org"; //1.1 写
httpRequest.Headers.Add("SOAPAction", action + "/" + txtMethodName.Text);
// //设置请求方法
httpRequest.Method = "POST";
//根据用户的输入构造SOAP封装,并把它保存到HTTP请求消息的流中
byte[] req_soap = this.BuildSoap();
//设置HTTP请求消息的ContentLength字段
httpRequest.ContentLength = req_soap.GetLength(0);
Stream rs = httpRequest.GetRequestStream();
rs.Write(req_soap, 0, (int)(httpRequest.ContentLength));
rs.Close();
}
catch(Exception ex)
{
MessageBox.Show(this, ex.Message + "\n堆栈内容:" + ex.StackTrace, "消息发送错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
} //显示HTTP请求消息的内容
DispRequest(httpRequest); ////////////////////////////////////////////
//从请求对象中获取响应对象 HttpWebResponse httpResponse;
bool err = false;
try
{
httpResponse = (HttpWebResponse)httpRequest.GetResponse();
lblResult.ForeColor = Color.Green;
}
catch (WebException ex)
{
MessageBox.Show(this, ex.Message + "\n堆栈内容:" + ex.StackTrace, "响应错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
httpResponse = (HttpWebResponse)ex.Response;
err = true;
lblResult.ForeColor = Color.Red;
} //获取响应消息主体的内容
string body = this.GetResponseBody(httpResponse);
lblResult.Text = this.GetResult(body, err); //显示响应消息的内容
DispResponse(httpResponse, body);
}我编译成功后输入参数 name="Tom"服务器只返回了结果 " Say Hello World" 。于是用网卡拦截器截获消息:这是请求:
Content-Type: text/xml; charset=utf-8
SOAPAction: http://tempuri.org/Hello
Host: 133.124.46.19
Content-Length: 306
Expect: 100-continue
Connection: Keep-Alive<?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>
<Hello xmlns="http://tempuri.org">
<name>Tom</name>
</Hello>
</soap:Body>
</soap:Envelope>这是响应:
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.1
Date: Mon, 14 Sep 2009 00:47:52 GMT
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Content-Length: 343<?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>
<HelloResponse xmlns="http://tempuri.org/">
<HelloResult> Say Hello World</HelloResult>
</HelloResponse>
</soap:Body>
</soap:Envelope>
经过多次实验,只要是带参数的webService方法,参数都丢失,希望高手帮助解决一下!!不胜感激!!
服务器端有WebService方法如下:
[WebMethod]
public string Hello(string name)
{
return name + " Say Hello World";
}-------------------------------------------------------------------------------------------------
创建Soap消息方法:
private byte[] BuildSoap()
{
//获取Web服务的命名空间
string service_nc;
if (txtNsOfService.Text != null && txtNsOfService.Text.Length > 0)
service_nc = txtNsOfService.Text;
else
service_nc = @"http://tempuri.org"; //创建只包含EnVelop元素的XML文档
XmlDocument doc = new XmlDocument();
string xmlStr = "<?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:Envelope>";
doc.LoadXml(xmlStr);
//"xmlns:soap='http://schemas.xmlsoap.org/soap/envelope'>" //soap 1.1 //获取文档顶级Envelope元素
XmlElement envelope = doc.DocumentElement; ///////////////////////////////////////////////
//创建并添加Header元素
if (lbxHeaderBlocks.Items.Count > 0)
{
XmlElement header = doc.CreateElement("soap", "Header", "http://schemas.xmlsoap.org/soap/envelope/");
foreach (object obj in lbxHeaderBlocks.Items)
{
HeaderBlock hb = (HeaderBlock)obj;
//创建报头元素
XmlElement block = doc.CreateElement(hb.Prefix, hb.Name, hb.Namespace);
if (hb.Actor != null)
block.SetAttribute("soap:actor", hb.Actor);
if (hb.MustUnderstand)
block.SetAttribute("soap:mustUnderstand", "true");
block.InnerXml = hb.Content; //把报头条目添加到Header元素中
header.AppendChild(block);
} //把Header元素添加到Envelope元素中
envelope.AppendChild(header);
} /////////////////////////////////////////////////////
//创建并添加body元素 if (txtMethodName.Text != null && txtMethodName.Text.Length > 0)
{
XmlElement body = doc.CreateElement("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/"); //向body元素中添加表示方法的结构元素
//创建方法结构元素
XmlElement method=doc.CreateElement(txtMethodName.Text,service_nc);
//向方法结构元素中添加表示参数的元素
foreach (string p in lbxParams.Items)
{
XmlElement elem = doc.CreateElement(p.Substring(0, p.IndexOf(":")),service_nc);
elem.InnerText = p.Substring(p.IndexOf(":") + 1);
method.AppendChild(elem);
}
//把方法元素添加到Body元素中
body.AppendChild(method);
//把Body元素添加到Envelope元素中
envelope.AppendChild(body);
} //把xml文档中的内容保存到byte数组中并返回
return Encoding.UTF8.GetBytes(doc.OuterXml);
}
-------------------------------------------------------------------------------------------------
窗体加入发送Soap消息 按钮事件:
private void btnSendMessage_Click(object sender, EventArgs e)
{
Uri theURL;
HttpWebRequest httpRequest;
try
{
theURL = new Uri(txtURI.Text);
}
catch (UriFormatException ex)
{
MessageBox.Show(this, ex.Message);
txtURI.Focus();
return;
} //以指定的URL创建请求对象
httpRequest = (HttpWebRequest)WebRequest.Create(theURL); //构造HTTP POST 消息,并把SOAP消息放到它的消息主体中
try
{
//根据用户的输入设置请求对象的标题字段
//httpRequest.ContentType = "application/soap+xml; charset=utf-8";
httpRequest.ContentType = "text/xml; charset=utf-8"; //添加SOAP Action 字段
string action;
if (txtNsOfService.Text != null && txtNsOfService.Text.Length > 0)
action = txtNsOfService.Text;
else
action = "http://tempuri.org"; //1.1 写
httpRequest.Headers.Add("SOAPAction", action + "/" + txtMethodName.Text);
// //设置请求方法
httpRequest.Method = "POST";
//根据用户的输入构造SOAP封装,并把它保存到HTTP请求消息的流中
byte[] req_soap = this.BuildSoap();
//设置HTTP请求消息的ContentLength字段
httpRequest.ContentLength = req_soap.GetLength(0);
Stream rs = httpRequest.GetRequestStream();
rs.Write(req_soap, 0, (int)(httpRequest.ContentLength));
rs.Close();
}
catch(Exception ex)
{
MessageBox.Show(this, ex.Message + "\n堆栈内容:" + ex.StackTrace, "消息发送错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
} //显示HTTP请求消息的内容
DispRequest(httpRequest); ////////////////////////////////////////////
//从请求对象中获取响应对象 HttpWebResponse httpResponse;
bool err = false;
try
{
httpResponse = (HttpWebResponse)httpRequest.GetResponse();
lblResult.ForeColor = Color.Green;
}
catch (WebException ex)
{
MessageBox.Show(this, ex.Message + "\n堆栈内容:" + ex.StackTrace, "响应错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
httpResponse = (HttpWebResponse)ex.Response;
err = true;
lblResult.ForeColor = Color.Red;
} //获取响应消息主体的内容
string body = this.GetResponseBody(httpResponse);
lblResult.Text = this.GetResult(body, err); //显示响应消息的内容
DispResponse(httpResponse, body);
}我编译成功后输入参数 name="Tom"服务器只返回了结果 " Say Hello World" 。于是用网卡拦截器截获消息:这是请求:
Content-Type: text/xml; charset=utf-8
SOAPAction: http://tempuri.org/Hello
Host: 133.124.46.19
Content-Length: 306
Expect: 100-continue
Connection: Keep-Alive<?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>
<Hello xmlns="http://tempuri.org">
<name>Tom</name>
</Hello>
</soap:Body>
</soap:Envelope>这是响应:
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.1
Date: Mon, 14 Sep 2009 00:47:52 GMT
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Content-Length: 343<?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>
<HelloResponse xmlns="http://tempuri.org/">
<HelloResult> Say Hello World</HelloResult>
</HelloResponse>
</soap:Body>
</soap:Envelope>
经过多次实验,只要是带参数的webService方法,参数都丢失,希望高手帮助解决一下!!不胜感激!!
解决方案 »
- 急救:有关WPF程序控件选中行背景色问题?
- SOS:C# Sokcet udp winform通信软件到其它没有装VS的机上通信不了
- 在C#中如何根据系统中安装的Word版本生成相应版本的word文档
- 关于获取公网IP的问题
- C#中如何让datagridview只显示当前插入的记录或删除的记录
- C#获取FTP服务器上的文件及文件夹属性
- 请教SQL问题,如何将数据库表字段的默认值写在SQL中?
- 欢迎大家下载我的软件,下载者有分.
- 能把一个.net写的组件(*.dll)嵌入到可执行文件里吗?
- sql语句请教 select count(*) from TB_News where IsDelete=1 and NewsTitle like '%108%'
- C# 实现完整的仿QQ截图控件
- 当有2个键盘的时候,怎么区别不同键盘的按键命令
第一个方法,其实就是返回的这个soap的二进制数组
<?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>
<Hello xmlns="http://tempuri.org">
<name>Tom </name>
</Hello>
</soap:Body>
</soap:Envelope>
---------------这里------------
byte[] req_soap = this.BuildSoap();
//设置HTTP请求消息的ContentLength字段
httpRequest.ContentLength = req_soap.GetLength(0);
Stream rs = httpRequest.GetRequestStream();
rs.Write(req_soap, 0, (int)(httpRequest.ContentLength));
rs.Close(); 主要是把soap 绑定到httpRequest 上面了
高手,请帮忙解决下!