【问题标题】:C# Xml in Http Post Request Message BodyHttp Post 请求消息正文中的 C# Xml
【发布时间】:2009-04-03 23:00:05
【问题描述】:

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

谢谢

【问题讨论】:

    标签: c# xml httpwebrequest httpwebresponse


    【解决方案1】:
    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();
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您应该使用WebRequest 类。

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

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

      【讨论】:

        猜你喜欢
        • 2017-10-03
        • 2017-01-27
        • 2023-04-07
        • 1970-01-01
        • 1970-01-01
        • 2017-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多