实现一个功能就是 我这里上传一个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);  
            }  
        }

解决方案 »

  1.   

    用Add service Reference或者Add web reference生成代理类更方便些吧。
      

  2.   

    WriteXml生成的xml文件肯定是不对的,应该是想下面这样的:<?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>
        <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>
      

  3.   

    为什么WriteXml生成的xml是不对的啊?我看了介绍这个是指使用指定的 Stream 以 XML 格式写入 DataTable 的当前内容
      

  4.   

    就是格式不对了,你要生成我3楼说的那种格式的xml,服务器才会认。 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"});  
    这个生成的应该是类似于下面这样的格式。
    <Project>
      <SIM>13216654124</SIM>
       ....
    </Project>如果你搞不好格式的话,就使用Add service Reference或者Add web reference生成代理类,直接使用类就可以了。
      

  5.   

    使用Add service Reference或者Add web reference生成代理类参考下面的文章的 "2.3、用ASP.NET调用Web Service" 小结:http://www.cnblogs.com/denylau/archive/2010/07/23/1783530.html