开发者

Problem while passing HashTable values as Web Request using Post Method

开发者 https://www.devze.com 2023-03-24 07:58 出处:网络
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.

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.GetRequestS开发者_如何学Pythontream()); 
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.


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号