传递XML文档作为参数的Web服务在C# [英] Passing XML document as an parameter to Web services in C#

查看:561
本文介绍了传递XML文档作为参数的Web服务在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要发送的XML文档作为参数来请求使用POST方法服务的WebRequest的。

I have to sent the XML document as an parameter to request an WebRequest from the Service using the Post method.

谁能帮是如何发送的XML文档作为参数,或者如何让字符串在整个文件中传递的文件。

Can anyone help be about how to sent the XML document as an parameter, or how to get the whole document in the string to pass as in as Document.

推荐答案

如果你想使用你需要做的是这样的一个名为表单参数来发表您的XML数据:

If you want to POST your Xml data using a named form parameter you need to do something like this:

HttpWebRequest request = HttpWebRequest.Create("http://yourdomain.com/whatever") as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

Encoding e = Encoding.GetEncoding("iso-8859-1");
XmlDocument doc = new XmlDocument();
doc.LoadXml("<foo><bar>baz</bar></foo>");
string rawXml = doc.OuterXml;

// you need to encode your Xml before you assign it to your parameter
// the POST parameter name is myxmldata
string requestText = string.Format("myxmldata={0}", HttpUtility.UrlEncode(rawXml, e));

Stream requestStream = request.GetRequestStream();
StreamWriter requestWriter = new StreamWriter(requestStream, e);
requestWriter.Write(requestText);
requestWriter.Close();

这篇关于传递XML文档作为参数的Web服务在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆