C# 如何通过 HTTP 请求发送 xml,并使用 ASP.NET MVC 接收它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18320916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:54:32  来源:igfitidea点击:

How to send xml through an HTTP request, and receive it using ASP.NET MVC?

c#asp.net-mvc-4posthttprequest

提问by Kalina

I am trying to send an xml string through an HTTP request, and receive it on the other end. On the receiving end, I am always getting that the xml is null. Can you tell me why that is?

我试图通过 HTTP 请求发送一个 xml 字符串,并在另一端接收它。在接收端,我总是得到 xml 为空。你能告诉我这是为什么吗?

Send:

发送:

    var url = "http://website.com";
    var postData = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><xml>...</xml>";
    byte[] bytes = System.Text.Encoding.ASCII.GetBytes(postData);

    var req = (HttpWebRequest)WebRequest.Create(url);

    req.ContentType = "text/xml";
    req.Method = "POST";
    req.ContentLength = bytes.Length;

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

    string response = "";

    using (System.Net.WebResponse resp = req.GetResponse())
    {
        using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
        {
            response = sr.ReadToEnd().Trim();
        }
     }

Receive:

收到:

[HttpPost]
[ValidateInput(false)]
public ActionResult Index(string xml)
{
    //xml is always null
    ...
    return View(model);
}

采纳答案by Kalina

I was able to get this working like so:

我能够像这样工作:

[HttpPost]
[ValidateInput(false)]
public ActionResult Index()
{
    string xml = "";
    if(Request.InputStream != null){
        StreamReader stream = new StreamReader(Request.InputStream);
        string x = stream.ReadToEnd();
        xml = HttpUtility.UrlDecode(x);
    }
    ...
    return View(model);
}

However, I am still curious why taking the xml as a parameter does not work.

但是,我还是很好奇为什么把xml作为参数不起作用。

回答by nomad

I believe this is because you've specified req.ContentType = "text/xml";.

我相信这是因为您已经指定了req.ContentType = "text/xml";.

If I remember correctly when you define your controller using a "primitive" type (stringbeing a "primitive" type here)

如果我没记错的话,当您使用“原始”类型(string此处为“原始”类型)定义控制器时

public ActionResult Index(string xml){}

MVC will try to look for xmleither in a query string or in the posted form data (html input field). But if you send something more complex to the server MVC will wrap it in a specific class.

MVC 将尝试xml在查询字符串或发布的表单数据(html 输入字段)中查找。但是如果你向服务器发送更复杂的东西,MVC 会将它包装在一个特定的类中。

For example, when you upload several files to the server you can accept them as follows in your controller

例如,当您将多个文件上传到服务器时,您可以在控制器中按如下方式接受它们

public ActionResult Index(IEnumerable<HttpPostedFileBase> files){}

So my guess is that you have to accept the text/xmlstream in the controller using the correct class.

所以我的猜测是你必须text/xml使用正确的类接受控制器中的流。

Update:

更新:

It seems there isn't such a class because you accept a data stream (and it's not coming from input element). You could write your own model binder to accept xml document. See discussions below.

似乎没有这样的类,因为您接受数据流(并且它不是来自输入元素)。您可以编写自己的模型绑定器来接受 xml 文档。请参阅下面的讨论。

Reading text/xml into a ASP.MVC Controller

将 text/xml 读入 ASP.MVC 控制器

How to pass XML as POST to an ActionResult in ASP MVC .NET

如何将 XML 作为 POST 传递给 ASP MVC .NET 中的 ActionResult