实现一个功能就是 我这里上传一个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);
}
}
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);
}
}
<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>
<SyncGPS xmlns="http://tempuri.org/">
<SIM>string</SIM>
<LPN>string</LPN>
<longitude>decimal</longitude>
<latitude>decimal</latitude>
<speed>decimal</speed>
<direction>decimal</direction>
<operationStatus>decimal</operationStatus>
<statusDesc>string</statusDesc>
<gpstime>string</gpstime>
</SyncGPS>
</soap:Body>
</soap:Envelope>
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"});
这个生成的应该是类似于下面这样的格式。
<Project>
<SIM>13216654124</SIM>
....
</Project>如果你搞不好格式的话,就使用Add service Reference或者Add web reference生成代理类,直接使用类就可以了。