C#写的WebService端怎么得到客户端调用时传来的XML数据
1个回答
展开全部
HttpContext.Current.Request.InputStream 就可以读出Xml内容。
// 创建一个DataTable
DataTable data = new DataTable("Project");
data.Columns.Add("Name");
data.Columns.Add("Birthday");
data.Rows.Add(new object[] { "Wendy", "1978/03/11" });
data.Rows.Add(new object[] { "Philip", "2000/11/05" });
data.Rows.Add(new object[] { "Felix", "1999/08/04" });
using (var ms = new MemoryStream())
{
// 将DataTable用Xml格式写入流
data.WriteXml(ms, XmlWriteMode.WriteSchema);
var client = new WebClient();
// 定义HttpRequest的Content-Type(xml,json等)
client.Headers.Add("Content-Type", "text/xml");
var url = ";
// Send HttpRequest
var resp = client.UploadData(url, "POST", ms.ToArray());
var strResp = System.Text.Encoding.UTF8.GetString(resp);
MessageBox.Show(strResp);
}
服务端:
[c-sharp] view plaincopy
[WebMethod]
public void SendXml()
{
// 获得客户端RAW HttpRequest
var inputStream = HttpContext.Current.Request.InputStream;
// 定义Response返回的格式是:Json
var response = HttpContext.Current.Response;
response.ContentType = "text/json";
//var strXml = "";
//using (var sr = new StreamReader(inputStream))
// strXml = sr.ReadToEnd();
try
{
DataTable data = new DataTable();
using (var xr = XmlReader.Create(inputStream))
data.ReadXml(xr);
// 将读入Xml的DataTable的行数返回客户端
string count = "/"" + data.Rows.Count + "/"";
response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes(count));
}
catch (Exception ex)
{
response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes(ex.Message));
}
}
// 创建一个DataTable
DataTable data = new DataTable("Project");
data.Columns.Add("Name");
data.Columns.Add("Birthday");
data.Rows.Add(new object[] { "Wendy", "1978/03/11" });
data.Rows.Add(new object[] { "Philip", "2000/11/05" });
data.Rows.Add(new object[] { "Felix", "1999/08/04" });
using (var ms = new MemoryStream())
{
// 将DataTable用Xml格式写入流
data.WriteXml(ms, XmlWriteMode.WriteSchema);
var client = new WebClient();
// 定义HttpRequest的Content-Type(xml,json等)
client.Headers.Add("Content-Type", "text/xml");
var url = ";
// Send HttpRequest
var resp = client.UploadData(url, "POST", ms.ToArray());
var strResp = System.Text.Encoding.UTF8.GetString(resp);
MessageBox.Show(strResp);
}
服务端:
[c-sharp] view plaincopy
[WebMethod]
public void SendXml()
{
// 获得客户端RAW HttpRequest
var inputStream = HttpContext.Current.Request.InputStream;
// 定义Response返回的格式是:Json
var response = HttpContext.Current.Response;
response.ContentType = "text/json";
//var strXml = "";
//using (var sr = new StreamReader(inputStream))
// strXml = sr.ReadToEnd();
try
{
DataTable data = new DataTable();
using (var xr = XmlReader.Create(inputStream))
data.ReadXml(xr);
// 将读入Xml的DataTable的行数返回客户端
string count = "/"" + data.Rows.Count + "/"";
response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes(count));
}
catch (Exception ex)
{
response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes(ex.Message));
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询