7

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?

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

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it also worked for me.. .If you are posting it as a stream you have to read input stream to get data.To Receive in "xml" vairable you must send this with query string as post parameter
0

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

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

public ActionResult Index(string xml){}

MVC will try to look for xml either 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.

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/xml stream in the controller using the correct class.

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.

Reading text/xml into a ASP.MVC Controller

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.