多个POST参数,一些包含XML

时间:2010-10-20 21:42:23

标签: c# asp.net

我希望能够让我的Web服务接受多个POST参数,其中一些参数将是XML。这可能吗?下面的代码将生成服务器错误:

WebResponse resp =(WebResponse)req.GetResponse();


string programId = "1";
        string statusMessages = statusMessagesXML.ToString(SaveOptions.DisableFormatting);

    string postData = "programId=" + programId;
    postData += "&statusMessages=" + HttpUtility.UrlEncode(statusMessages);
    string data = postData;

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
    req.ContentType = "application/x-www-form-urlencoded";
    req.ContentLength = data.Length;
    req.Method = "POST";
    StreamWriter writer = new StreamWriter(req.GetRequestStream());
    writer.Write(data);
    writer.Flush();
    writer.Close();

    String result = null;
    WebResponse resp = (WebResponse)req.GetResponse();
    Stream readstream = resp.GetResponseStream();
    StreamReader read = new StreamReader(readstream);

    result = read.ReadToEnd();

感谢。

1 个答案:

答案 0 :(得分:-1)

问题是:

req.ContentType = "application/x-www-form-urlencoded";

这告诉服务器您的数据是URL编码的,'<'是一个URL元字符。 URLEncode您的数据,或者不告诉服务器数据是URLEncoded,而不是。