为什么我的http post方法不接受xml字符?

时间:2014-12-18 05:58:33

标签: c# xml web-services asmx

这是功能:

 private void button6_Click(object sender, EventArgs e1)
    {

        string requestText = string.Format("strXMLData={0}", System.Web.HttpUtility.UrlEncode("<tag1>text</tag1>", e));

       string data = "strXMLData=%3c&strXMLFileName=text1.xml";  //Working I am //getting in service mathod <

string data = "strXMLData=%3e&strXMLFileName=text1.xml";  //Working I am getting in service mathod >

//string data = "strXMLData=%3c%3e&strXMLFileName=text1.xml"; //this is also working,I am getting in service mathod  
//string data = "strXMLData=%3ct%3e&strXMLFileName=text1.xml"; //this is not working,I am getting error 500, service mathod should revcive either same string or <t>
        byte[] dataStream = Encoding.GetEncoding("iso-8859-1").GetBytes(data);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:52995/MyWebService.asmx/ReceiveXMLByContent");
        request.Method = "POST";
      request.ContentType = "application/x-www-form-urlencoded";
       // request.ContentType = "multipart/form-data";

        request.ContentLength = dataStream.Length;
        Stream newStream = request.GetRequestStream();
        newStream.Write(dataStream, 0, dataStream.Length);
        newStream.Close();
        var reader = new System.IO.StreamReader(request.GetResponse().GetResponseStream());
        string dataReturn = reader.ReadToEnd();
    }

在上面的代码中我写了3个案例,其中两个正在工作,第三个案例

string data = "strXMLData=%3ct%3e&strXMLFileName=text1.xml"; //this is not working,I am getting error 500, service mathod should revcive either same string or <t>  

不工作你能解释为什么它没有传递xml字符串,我试图传递

<tag1>
value
</tag1>

因为我们无法在没有编码的情况下传递xml所以我使用

编码了这个字符串
  string requestText = string.Format( System.Web.HttpUtility.UrlEncode("<tag1>text</tag1>", e)); //which returns %3ctag1%3etext%3c%2ftag1%3e  

你能解释一下如何传递xml字符串..? 没有得到错误500

这是网络服务方法

  [WebMethod]
  public string ReceiveXMLByContent(string strXMLData, string strXMLFileName)
    {
        string b = System.Web.HttpUtility.UrlDecode(strXMLData);            
        return "worked";
    }

1 个答案:

答案 0 :(得分:0)

问题总是存在以下几行

        byte[] dataStream = Encoding.GetEncoding("iso-8859-1").GetBytes(data);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:52995/MyWebService.asmx/ReceiveXMLByContent");
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentType = "multipart/form-data";

确保request.ContentType特别合适,如下所示:

        request.ContentType = "text/xml; charset=\"utf-8\"; action=\"HeaderName\";";

确保使用这样的try和catch方法:

 private void button6_Click(object sender, EventArgs e1)
 {
   string GetHttpPost = string.Empty;
   GetHttpPost = CallHTTPPostMethod(); 
 }

 public string CallHTTPPostMethod()
 {
   try
   {
     //Your code

     return YourResponseXMLInStringFormat;
   }
   catch(Exception wex)
   {
     string pageContent = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd().ToString();
     return pageContent;
   } 
  }