I am trying to send data to an MVC controller, but have not been successful so far.
Here is the message data structure:
private string makeHttpPostString(XmlDocument interchangeFile) { string postDataString = "uid={0}&localization={1}&label={2}&interchangeDocument={3}"; InterchangeDocument interchangeDocument = new InterchangeDocument(interchangeFile); using (var stringWriter = new StringWriter()) using (var xmlTextWriter = XmlWriter.Create(stringWriter)) { interchangeFile.WriteTo(xmlTextWriter); string interchangeXml = HttpUtility.UrlEncode(stringWriter.GetStringBuilder().ToString()); string hwid = interchangeDocument.DocumentKey.Hwid; string localization = interchangeDocument.DocumentKey.Localization.ToString(); string label = ConfigurationManager.AppSettings["PreviewLabel"]; return (string.Format(postDataString, hwid, localization, label, interchangeXml)); } }
Here is the request:
HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create(controllerUrl); webRequest.Method = "POST"; // webRequest.ContentType = "application/x-www-form-urlencoded"; string postData = makeHttpPostString(interchangeFile); byte[] byteArray = Encoding.UTF8.GetBytes(postData); webRequest.ContentLength = byteArray.Length; using (Stream dataStream = webRequest.GetRequestStream()) { dataStream.Write(byteArray, 0, byteArray.Length); } HttpWebResponse webresponse = (HttpWebResponse) webRequest.GetResponse();
When I set the request content type to "application / x-www-form-urlencoded" GetReponse () fails with a 500 server error code. When I comment on this and only httpencode the xml data, "interchangeXml", the message is sent, but only the third parameter, "label" reaches the controller. The rest are null.
What is the correct way to send values ββto a controller action when one of these values ββis xml data?
Thanks!
Update
I am sending the entire parameter except XML via the query string. However, the problem now is that I do not know how to access published data in the controller action. Can someone tell me how I access xml from HttpRequest from my controller action?
Update
I reworked the code above to use the suggestions Darin made to me. I get an internal server error (500) using WebClient UploadValues ββ().
Act:
[AcceptVerbs(HttpVerbs.Post)] public ActionResult BuildPreview(PreviewViewModel model) { ... }
Inquiry:
private string PostToSxController(XmlDocument interchangeFile, string controllerUrl) { var xmlInterchange = new InterchangeDocument(interchangeFile); using (var client = new WebClient()) { var values = new NameValueCollection() { {"uid", xmlInterchange.DocumentKey.Hwid}, {"localization", xmlInterchange.DocumentKey.Localization.ToString()}, {"label", ConfigurationManager.AppSettings["PreviewLabel"]}, {"interchangeDocument", interchangeFile.OuterXml } }; byte[] result = null; try { result = client.UploadValues(controllerUrl, values); } catch(WebException ex) { var errorResponse = ex.Response; var errorMessage = ex.Message; } Encoding encoding = Encoding.UTF8; return encoding.GetString(result); } }
Route:
routes.MapRoute( "BuildPreview", "SymptomTopics/BuildPreview/{model}", new { controller = "SymptomTopics", action = "BuildPreview", model = UrlParameter.Optional } );