C#写的WebService端如何得到客户端调用时传来的XML数据?

BarnettYu 2011-03-25 03:48:44
StreamReader srd = new StreamReader(HttpContext.Current.Request.InputStream);
string strReq = srd.ReadToEnd();

尝试了这样子拿不到的样子?哪位大侠分享一下?
...全文
501 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
北极地之光 2012-07-12
  • 打赏
  • 举报
回复
学习了,最近也在学这方面知识
机器人 2011-03-25
  • 打赏
  • 举报
回复
转了半天又转回来。我还是觉得服务端直接定义为
[WebMethod]
public void SendOrder(string strXml)
{
File.WriteAllText("test.xml", strXml);
}
就可以了。


客户端的Request,如下:
POST /Service1.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/SendOrder"

<?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>
<SendOrder xmlns="http://tempuri.org/">
<strXml>string</strXml>
</SendOrder>
</soap:Body>
</soap:Envelope>

也就是说只要满足Soap协议的xml格式都可以通过POST直接请求服务。
服务的方法以及参数已经包含在Xml数据中。

现在不知道你的Xml内容是固定的,还是随意?
BarnettYu 2011-03-25
  • 打赏
  • 举报
回复
非常感谢,我也在继续查阅资料中,结贴了,如果你有新的信息,麻烦给我邮件
6346111@qq.com
机器人 2011-03-25
  • 打赏
  • 举报
回复
现在的REST WCF的可以做到。。。而且如果数据格式好的话,你可以直接拿到反序列化的Order对象。
在WCF里是WebOperation.Current.IncomingRequest, webservcie的话,我试试。。。

http://blog.csdn.net/fangxinggood/archive/2011/03/14/6247297.aspx
BarnettYu 2011-03-25
  • 打赏
  • 举报
回复
[WebMethod]
public void SendOrder(string orderXml)
{

}

这样子不行的样子,因为客户端调用的时候并没有把那些XML放到一个参数里面再传过来,而且直接发了报文过来

所以我在服务端里的代码是这样子

[WebMethod]
public void SendOrder()
{
}

这个是因为我在调用其它公司的时候客户端是类似我上面写的那样子,只需要发报文过去就OK了。
机器人 2011-03-25
  • 打赏
  • 举报
回复
是先有了客户端,现在再定义服务的意思吗?
这样你收到的就是xml字符串。

[WebMethod]
public void SendOrder(string orderXml)
{

}
BarnettYu 2011-03-25
  • 打赏
  • 举报
回复
现在的问题就在于,我服务端应该怎么样来获得从客户端传过来的XML数据,就是1f8cb146-44a1-4d47-ad31-249e7e5f1130.xml里的数据。
BarnettYu 2011-03-25
  • 打赏
  • 举报
回复
客户端传XML的代码如下:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("/1f8cb146-44a1-4d47-ad31-249e7e5f1130.xml"));

byte[] bytes = Encoding.UTF8.GetBytes(xmlDoc.ToString());
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://localhost/WebService.asmx/SendOrder");
Encoding encoding = Encoding.GetEncoding("utf-8");

myRequest.Method = "POST";
myRequest.ContentType = "text/xml";
myRequest.ContentLength = bytes.Length;
Stream newStream = myRequest.GetRequestStream();
// 发送数据
newStream.Write(bytes, 0, bytes.Length);
newStream.Close();

Stream responseStream;
string stringResponse = string.Empty;

try
{

responseStream = myRequest.GetResponse().GetResponseStream();

using (StreamReader responseReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")))
{
stringResponse = responseReader.ReadToEnd();
}
responseStream.Close();
Response.Write(stringResponse.ToString());
}
catch(Exception ee)
{
Response.Write(ee.Message);
}
BarnettYu 2011-03-25
  • 打赏
  • 举报
回复
我是需要在WebService的服务端获得客户端传来的XML数据,你的代码好像不太对路啊
机器人 2011-03-25
  • 打赏
  • 举报
回复
你的Service方法怎么写的?贴出来看看。

WebService用成这样。。。也太别扭了。。。直接用Asp.net页面好了。。。
小童 2011-03-25
  • 打赏
  • 举报
回复
   var xml = @"<duliye>
<news>
<id>1</id>
<title>介绍</title>
<content>
介绍介绍介绍介绍介绍介绍介绍
</content>
</news>
<news>
<id>2</id>
<title>团队</title>
<content>
我们的团队我们的团队
</content>
</news>
<news>
<id>3</id>
<title>大事件</title>
<content>
2008年7月
2010年9月
</content>
</news>
</duliye>";

var doc = new XmlDocument();
doc.LoadXml(xml);

var node = doc.SelectSingleNode("/duliye/news[child::id[text()=2]]");//找到id=2的news节点,之后你就可以去它的ChildNodes里取你想要的内容了

Console.WriteLine(string.Format(@"news:
Id={0}
title={1}
content={2}", node.ChildNodes[0].InnerText, node.ChildNodes[1].InnerText, node.ChildNodes[2].InnerText));

110,524

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧