0

I am developing a web form in asp.net using C# to post some data to other site. I am experiencing a problem while passing data stored in Hash Table using StreamWriter. Following is the code snippet which I am using to store data in HashTable and posting to other site using HttpWebRequest. 1) //Hash Table to store data

Hashtable
post_parameters = new Hashtable(); 
post_parameters.Add("format", "atom");
post_parameters.Add("user[first_name]", "ABC"); 
post_parameters.Add("user[last_name]", "XYZ"); 
post_parameters.Add("client_id", "1111111"); 
post_parameters.Add("user[salutation]", "Mr."); 
post_parameters.Add("user[account_attributes][addresses_attributes][0][street]", "Street"); 
post_parameters.Add("user[account_attributes][addresses_attributes][0][street2]", "Street2"); 
post_parameters.Add("user[account_attributes][addresses_attributes][0][city]", "New York");
post_parameters.Add("user[account_attributes][addresses_attributes][0][state]", "NY"); 
post_parameters.Add("user[account_attributes][addresses_attributes][0][postal_code]", "10017"); 
post_parameters.Add("user[account_attributes][addresses_attributes][0][country_code]", "US"); 
post_parameters.Add("user[mapbuzz_auth_attributes][email]", [email protected]); 
post_parameters.Add("user[employee_attributes][position]", "Consultant"); 
post_parameters.Add("user[employee_attributes][company_attributes][name]", "XYZ");

2) //Method to send data using HttpWebRequest

Uri uri = new Uri(http://www.xyz.com/user + "?" + query_string); 
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.Method = WebRequestMethods.Http.Post;
request.ContentType = 
"application/x-www-form-urlencoded"; 
StreamWriter writer = new StreamWriter(request.GetRequestStream()); 
writer.Write(post_parameter);
writer.Close();
HttpWebResponse
response = (HttpWebResponse)request.GetResponse(); 
StreamReader reader = new StreamReader(response.GetResponseStream()); 
string tmp = reader.ReadToEnd(); 
response.Close();
Response.Write(tmp);

Problem:

I get a response as "422 unprocessable entity" for passing HashTable as a parameter. Please provide help on this and let me know how to pass Hashtable as a Web Request.

1
  • Are you trying to achieve this ? foreach (var item in post_parameters) writer.Write(item.GetHashCode());
    – cgon
    Commented Aug 2, 2011 at 12:54

2 Answers 2

1

From the wiki article on HTTP POST, the following can be read

When a web browser sends a POST request from a web form element, the standard Internet media type is "application/x-www-form-urlencoded". This is a format for encoding key-value pairs with possibly duplicate keys. Each key-value pair is separated by an '&' character, and each key is separated from its value by an '=' character. Keys and values are both escaped by replacing spaces with the '+' character and then using URL encoding on all other non-alphanumeric characters.

So, to take your hashtable, you need to write that into the body of the request as

format=atom&user[first_name]=ABC&user[last_name]=XYZ.... /etc

You need to plug this into this piece of your code:

StreamWriter writer = new StreamWriter(request.GetRequestStream()); 
writer.Write("format=atom&user[first_name]=ABC&user[last_name]=XYZ");
writer.Close();

And finally, an easy way to turn your HashTable into that form or string:

String.Join("&", post_parameters.OfType<DictionaryEntry>().Select(de => String.Format("{0}={1}", de.Key, de.Value)));
0

You are only sending the name of the class which is System.Collections.Hashtable right now. Maybe your purpose is to achieve this:

    foreach (var item in post_parameters)
    {
        writer.Write(item.GetHashCode());
    }
4
  • Thanks for your quick response!!! But it is not which i am looking for. actually i am trying to replicate a code written in ruby as "request.set_form_data(post_parameters)" to C#. this code just sets the data which is to be posted with http to a hashtable named as post_parameters. Give me somthing which i can use in C# as it is there in ruby to post hashtable.
    – Rupesh
    Commented Aug 2, 2011 at 13:14
  • I am sorry. I did not read your question carefully. Your purpose is posting a form. So the only thing I have for you is reading your hashtable line by line write it line by line enclosing them with boundary. Take a look at this: stackoverflow.com/questions/566462/… You need to use "multipart/form-data; boundary=" instead of application/x-www-form-urlencoded. I do not know any built in function to do the ruby job for you. Thanks.
    – cgon
    Commented Aug 2, 2011 at 13:23
  • The site where I am sending this data has a restriction of sending the data in "application/x-www-form-urlencoded" only.
    – Rupesh
    Commented Aug 2, 2011 at 14:57
  • Is it possible to tell what you see on fiddler(fiddler2.com/fiddler2) when requesting the web page with your ruby code? Especially the raw view of your request. Thanks.
    – cgon
    Commented Aug 2, 2011 at 15:32

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.