c# - 如何在xml中返回GetResponseStream?

标签 c# asp.net xml web-services

我正在尝试创建 Web 请求,该请求通过 POST 调用发送 XML,并希望以 XML 格式返回响应。

我在回复 xml 时遇到了一点困难,因为我还小,我不确定如何在下面的代码中设置它。这是我的尝试:

      // Attempt to receive the WebResponse to the WebRequest.
            using (HttpWebResponse hwresponse = (HttpWebResponse)hwrequest.GetResponse())
            {
                statusCode = (int)hwresponse.StatusCode;
                if (hwresponse != null)
                { // If we have valid WebResponse then read it.
                    using (StreamReader reader = new StreamReader(hwresponse.GetResponseStream()))
                    {
                        // XPathDocument doc = new XPathDocument(reader);
                        string responseString = reader.ReadToEnd();
                        if (statusCode == 201 )
                        {

                          //  var response = new XElement("Status",
                           //    new XElement("status_code", statusCode),
                          //     new XElement("resources_created",
                          ////         new XElement("Link"),
                          //         new XElement("href"),
                          //         new XElement("title")
                          //         ),

                          //         new XElement("warnings")

                           //        );

                            XmlDocument xmlDoc = new XmlDocument();
                            xmlDoc.Load(responseString);
                            XmlNodeList address = xmlDoc.GetElementsByTagName("Status");

                            responseData = xmlDoc.ToString();
                            reader.Close();

                        }
                    }
                }

                hwresponse.Close();

            }

        }
        catch (WebException e)
        {
            if (e.Status == WebExceptionStatus.ProtocolError)
            {
               // XmlDocument xmlDoc = new XmlDocument();
              //  XmlNodeList address = xmlDoc.GetElementsByTagName("Status", statusCode);
               // xmlDoc.Load(xmlDoc);
            }



         //   if (e.Status == WebExceptionStatus.ProtocolError)
         //   {
              //  responseData = "Status Code : {0}" + ((HttpWebResponse)e.Response).StatusCode + "Status Description : {0}" + ((HttpWebResponse)e.Response).StatusDescription;
                // responseData "Status Description : {0}" + ((HttpWebResponse)e.Response).StatusDescription;


           // }
        }

我希望能够以下列 XML 格式返回响应:

<status>
<status_code>201</status_code>
<etag>12345678</etag>
<resources_created>
    <link 
        rel="http://api-info.com" 
        href="http://api-info.com/tag/Some%20Tag" 
        title="Subscriber Tag (Some Tag)" />
</resources_created>
<warnings>
    <warning>Some Warning Message</warning>
</warnings>
</status>

我还想问一下,我的'StatusCode'是否应该设置为条件或try&catch。
任何指南都会很有帮助。非常感谢。

最佳答案

您可能无法控制发送给您的内容,但您可以请求带有 Accept header 的 xml。

hwrequest.Accept = "application/xml";

但是,您无法控制该结构。

关于c# - 如何在xml中返回GetResponseStream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25991516/

相关文章:

c# - 有没有一种可靠的方法来检测浏览器是否在 C# 中启用了 javascript?

c# - 订阅其他项目中的 Controller 引发的事件

java - 将 XML 文档解析为 Java 对象的方法

xml - 从 XSL 中的 CDATA 标记中呈现 HTML 标记

jquery - 在 XMLHttpRequest 中使用变量名

c# - ASP.NET MVC WebAPI 从异步任务创建 ViewModel

c# - 模型绑定(bind)新的 Datatables 1.10 参数

c# - 有没有一种方法可以组合 2 个 "FileSystem.CopyDirectory"语句,以便我只得到一个进度窗口?

c# - 似乎无法通过 ASP.Net Core 连接到 SQL Server(一直返回空值)

c# - 如何高效地将大文件从数据库发送到浏览器?