3

I am trying to post some data to a ASP.NET MVC Controller Action. Current I am trying to use WebClient.UploadData() to post several parameters to my action.

The following will hit the action but all the parameters are null. How can get the posted data from the http request?

string postFormat = "hwid={0}&label={1}&interchange={2}localization={3}";
var hwid = interchangeDocument.DocumentKey.Hwid;
var interchange = HttpUtility.UrlEncode(sw.ToString());
var label = ConfigurationManager.AppSettings["PreviewLabel"];
var localization = interchangeDocument.DocumentKey.Localization.ToString();

string postData = string.Format(postFormat, hwid, interchange, label, localization);

using(WebClient client = new WebClient())
{
   client.Encoding = Encoding.UTF8;
   client.Credentials = CredentialCache.DefaultNetworkCredentials;
   byte[] postArray = Encoding.ASCII.GetBytes(postData);
   client.Headers.Add("Content-Type", "pplication/x-www-form-urlencoded");
   byte[] reponseArray = client.UploadData("http://localhost:6355/SymptomTopics/BuildPreview",postArray);
   var result = Encoding.ASCII.GetString(reponseArray);
   return result;
}

Here is the Action I am calling

public ActionResult BuildPreview(string hwid, string label, string interchange, string localization) { ... }

When this Action is reached all the parameters are null.

I have tried using the WebClient.UploadValue() and passing the data as a NameValueCollection. This method always returns a status of 500 and because I am making this http request from within the MVC application I cannot find a way to bebug this.

Any help getting this resolved would be super helpful.

-Nick

I corrected the Header to read:

client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

Now UploadData just errors immediately with with server error 500.

6
  • your content type looks broken. You're missing an 'a' at the front of pplication/x-www-form-urlencoded. Commented Jun 9, 2011 at 2:52
  • Thanks.. so when I correct my typo UploadData() just errors with a 500 Commented Jun 9, 2011 at 3:04
  • Do you have any special routes defined? Commented Jun 9, 2011 at 4:28
  • Show us the 500 exception details Commented Jun 9, 2011 at 7:40
  • My route looks just like this: routes.MapRoute( "BuildPreview", "SymptomTopics/BuildPreview/{hwid}/{label}/{localization}", new { controller = "SymptomTopics", action = "BuildPreview"} ); Commented Jun 9, 2011 at 14:47

4 Answers 4

5

Just for laughs have a look in Request.Form and the RouteData in your controller to see if something ended up there.

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

1 Comment

Agree, should be the accepted answer. Here is an example for newbies: string somethingFromAField = Request.Form.Get("someTextField"); - the "someTextField" is from the form that we just sent
3

I was able to get the post xml data from the Request objects InputStream property.

      public ActionResult BuildPreview(string hwid, string label, string localization)
         {
             StreamReader streamReader = new StreamReader(Request.InputStream);
             XmlDocument xmlDocument = new XmlDocument();
             xmlDocument.LoadXml(streamReader.ReadToEnd());
               ... 

 }

Comments

2

As a stop-gap measure, you can always change your controller action to accept a FormCollection parameter and then reach in and access the form parameters by name directly.

Comments

1

To get the raw posted bytes from WebClient.UploadData("http://somewhere/BuildPreview", bytes)

public ActionResult BuildPreview()
{
    byte[] b;
    using (MemoryStream ms = new MemoryStream())
    {
        Request.InputStream.CopyTo(ms);
        b = ms.ToArray();
    }

    ...
}

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.