xml菜鸟问题
实现一个功能就是 我这里上传一个xml数据给对方服务器的webservice,但是断点调试发现,执行到 var resp = client.UploadData(url, "POST", ms.ToArray()); 这一步就报错: 远程服务器返回错误: (500) 内部服务器错误。
代码如下:
private void Form1_Load(object sender, EventArgs e)
{
DataTable data = new DataTable("Project");
data.Columns.Add("SIM");
data.Columns.Add("LPN");
data.Columns.Add("longitude");
data.Columns.Add("latitude");
data.Columns.Add("speed");
data.Columns.Add("direction");
data.Columns.Add("operationStatus");
data.Columns.Add("statusDesc");
data.Columns.Add("gpstime");
data.Rows.Add(new object[] { "13216654124","", 121.25089, 30.16242, 50, 102, 0, "", "2012-06-27 15:22:52"});
using (var ms = new MemoryStream())
{
// 将DataTable用Xml格式写入流
data.WriteXml(ms, XmlWriteMode.WriteSchema);
WebClient client = new WebClient();
// 定义HttpRequest的Content-Type(xml,json等)
client.Headers.Add("Content-Type", "text/xml");
string url = "http://61.164.66.141:8082/GPSUploadService.asmx?op=SyncGPS";
// Send HttpRequest
var resp = client.UploadData(url, "POST", ms.ToArray());
var strResp = System.Text.Encoding.UTF8.GetString(resp);
MessageBox.Show(strResp);
}
}
是不是我生成的xml有问题,我看了下不是 soap格式,本人菜鸟,如何写才可以变成soap格式的xml呢?
------解决方案--------------------
1. 直接添加ServiceReference,就可以直接以对象方式调用了。
var client = new GPSService.GPSUploadServiceSoapClient();
client.SyncGPS("13216654124","", 121.25089, 30.16242, 50, 102, 0, "", "2012-06-27 15:22:52");
2. 用post如下:
C# code
var client = new WebClient();
client.Headers.Add("SOAPAction: \"http://tempuri.org/SyncGPS\"");
client.Headers.Add("Content-Type: text/xml; charset=utf-8");
var data = new DataTable("SyncGPS");
data.Columns.Add("SIM");
data.Columns.Add("LPN");
data.Columns.Add("longitude");
data.Columns.Add("latitude");
data.Columns.Add("speed");
data.Columns.Add("direction");
data.Columns.Add("operationStatus");
data.Columns.Add("statusDesc");
data.Columns.Add("gpstime");
data.Rows.Add(new object[] { "13216654124", "", 121.25089, 30.16242, 50, 102, 0, "", "2012-06-27 15:22:52" });
using (var sw = new StringWriter())
{
data.WriteXml(sw);
var body = sw.ToString();
var xdoc = new XmlDocument();
xdoc.LoadXml(body);
body = xdoc.FirstChild.InnerXml;
var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
xml += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
xml += "<s:Body>";
xml += body;
xml += "</s:Body>";
xml += "</s:Envelope>";
var result = client.UploadString("http://61.164.66.141:8082/GPSUploadService.asmx", "POST", xml);
Console.WriteLine(resu