繁体   English   中英

Http Post Request Message Body中的C#Xml

[英]C# Xml in Http Post Request Message Body

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

谢谢

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();
        }
    }
}

您应该使用WebRequest类。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM