Http Post Request Message Body中的C#Xml

jum*_*ojs 12 c# xml httpwebrequest httpwebresponse

我正在寻找一个示例,说明如何在C#中将xml文档放在http请求的消息体中,然后解析响应.我已经阅读了文档,但我想看一个例子,如果有一个可用的.有人有例子吗?

谢谢

Gra*_*tzy 30

private static string WebRequestPostData(string url, string postData)
{
    System.Net.WebRequest req = System.Net.WebRequest.Create(url);

    req.ContentType = "text/xml";
    req.Method = "POST";

    byte[] bytes = System.Text.Encoding.ASCII.GetBytes(postData);
    req.ContentLength = bytes.Length;

    using (Stream os = req.GetRequestStream())
    {
        os.Write(bytes, 0, bytes.Length);
    }

    using (System.Net.WebResponse resp = req.GetResponse())
    {
        if (resp == null) return null;

        using (System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream()))
        {
            return sr.ReadToEnd().Trim();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Meh*_*ari 5

你应该使用这个WebRequest班级.

这里有一个带注释的示例可用于发送数据:

http://msdn.microsoft.com/en-us/library/debx8sh9.aspx