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]", abc.xyz@xyz.com); 
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.

Rupesh
  • 1
  • 1
  • Are you trying to achieve this ? foreach (var item in post_parameters) writer.Write(item.GetHashCode()); – cgon Aug 02 '11 at 12:54

2 Answers2

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)));
Jamiec
  • 133,658
  • 13
  • 134
  • 193
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());
    }
cgon
  • 1,955
  • 2
  • 23
  • 37
  • 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 Aug 02 '11 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: http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data 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 Aug 02 '11 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 Aug 02 '11 at 14:57
  • Is it possible to tell what you see on fiddler(http://www.fiddler2.com/fiddler2/) when requesting the web page with your ruby code? Especially the raw view of your request. Thanks. – cgon Aug 02 '11 at 15:32