Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Post request hit to server successfully, then server check the parameter it says null. Now anybody can say what wrong with me and what should i do..PLEASE HELP ME, THANKS IN ADVANCED.
C#
client = new HttpClient();
                    client.DefaultRequestHeaders.Add("User-Agent", "RingtoneApplication");
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.BaseAddress = new Uri("http://192.168.137.242/");
                    HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, "new_ringtone");              
                    Object[] postDataObject= CreateData();
                    string postDataJson = JsonConvert.SerializeObject(postDataObject);
                    string tokenData = CreateToken(postDataJson, "123");
                    string parameters = "method=getRintoneCategories&data=" + postDataJson + "&token=" + tokenData;
                    string jsonData = JsonConvert.SerializeObject(parameters);
                    req.Content = new StringContent(parameters, Encoding.UTF8, "application/json");
    
                    client.SendAsync(req).ContinueWith(respTask =>
                    {
                        Debug.WriteLine("Response: {0}", respTask.Result);
                    });
Posted
Updated 7-Aug-14 10:32am
v2
Comments
Sergey Alexandrovich Kryukov 7-Aug-14 14:22pm    
What are the values of getRintoneCategories, postDataJson and tokenData. Are any characters which should be escaped? Also, use string.Format instead of repeated '+', or use System.Text.StringBuilder to build string with variable number of parameters.
—SA
suzand 8-Aug-14 4:20am    
getRingtoneCategories nothing but a string. if you look at the value of "parameters" i think you can understand easily:
method=getRintoneCategories&data=[{"user":{"Device":"XDeviceEmulator","OSVersion":"80103","FetchTime":"8/8/2014"}},{"Ringtoneapp":"category_id"}]&token=Skrru9+RMVYpY+82vMIacolw8XpOABtKMw7waBXvBm8=
Sergey Alexandrovich Kryukov 8-Aug-14 4:51am    
Maybe some of these punctuation characters should be escaped as they confuse the parser of HTTP data; I did not thoroughly examine the structure you show, this is just the idea. To experiment with that and get understanding of the problem, try to send something simpler, such as:
method=getRintoneCategories&data=12345&token=678
Send it and see what happens.
—SA
suzand 8-Aug-14 9:24am    
I've tried the way you mentioned but same problem.
Sergey Alexandrovich Kryukov 8-Aug-14 13:36pm    
Aha...

1 solution

XML
client = new HttpClient();
                    client.DefaultRequestHeaders.Add("User-Agent", "RingtoneApplication");
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    Object[] dataObj = CreateData();
                    string dataString = JsonConvert.SerializeObject(dataObj);
                    var values = new List<KeyValuePair<string, string>>();
                    values.Add(new KeyValuePair<string, string>("data", dataString));
                    values.Add(new KeyValuePair<string, string>("method", "getRintoneCategories"));
                    string token = CreateToken(dataString, "123");
                    values.Add(new KeyValuePair<string, string>("token", token));
                    var content = new FormUrlEncodedContent(values);
                    var response = await client.PostAsync("http://192.168.0.103/new_ringtone/", content);
                    responseString = await response.Content.ReadAsStringAsync();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900