locked
HttpFormUrlEncodedContent object not encoding the paremeters

    Question

  • Im developing windows universal store app where it communicates to web api.
    Previously I was using "System.Net.Http" api for communication.Now changed the code and using "Windows.web.Http".

    I am facing problem with HttpFormUrlEncodedContent object in my application for a PostAsync method of Windows.Web.Http.HttpClient.

    Code Snippet :
    ------------------
    IHttpContent content = null;
    content = new HttpFormUrlEncodedContent(postData);// postData (IDictionary<string, string> postData)

    Problem: postData is not encoding.
    Result : FIRST_PARAMETER%2BSECOND_PARAMETER%2BTHIRD_PARAMETER

    Working code snippet using "System.Net.Http"
    ---------------------------------------------
    HttpContent content = null;
    content = new FormUrlEncodedContent(postData);

    Is any other way of defining the content of my PostAsync method?


    Thanks.


    Wednesday, April 8, 2015 10:41 AM

Answers

  • Can you give more explicit information on what parameters are you actually trying to pass in? For example, if I use the below code, then both HTTP requests send the POST request using the same header & data:

    A.) The HTTP header: Content-Type: application/x-www-form-urlencoded

    B.) The HTTP body: "name=John+Doe&age=45&test=A%2BB%3DC" (without quotes)

    private async void btnSendRequest_Click(object sender, RoutedEventArgs e)
            {
                txtOut.Text = "";
                try
                {
                    Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient();
                    Uri uri = new Uri("http://127.0.0.1/time.aspx");
                    List<KeyValuePair<string, string>> formData = new List<KeyValuePair<string, string>>();
                    formData.Add(new KeyValuePair<string, string>("name", "John Doe"));
                    formData.Add(new KeyValuePair<string, string>("age", "45"));
                    formData.Add(new KeyValuePair<string, string>("test", "A+B=C"));
                    
                    Windows.Web.Http.HttpFormUrlEncodedContent content = new Windows.Web.Http.HttpFormUrlEncodedContent(formData);
                    Windows.Web.Http.HttpResponseMessage response = await client.PostAsync(uri, content);
                    String responseString = await response.Content.ReadAsStringAsync();
                    txtOut.Text = responseString;
                }
                catch (Exception oEx)
                {
                    txtOut.Text = "Exception: " + oEx.Message;
                }
            }
    
            private async void btnSendRequest_old_Click(object sender, RoutedEventArgs e)
            {
                txtOut.Text = "";
                try
                {
                    System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
                    Uri uri = new Uri("http://127.0.0.1/time.aspx");
                    List<KeyValuePair<string, string>> formData = new List<KeyValuePair<string, string>>();
                    formData.Add(new KeyValuePair<string, string>("name", "John Doe"));
                    formData.Add(new KeyValuePair<string, string>("age", "45"));
                    formData.Add(new KeyValuePair<string, string>("test", "A+B=C"));
    
                    System.Net.Http.FormUrlEncodedContent content = new System.Net.Http.FormUrlEncodedContent(formData);
    
                    System.Net.Http.HttpResponseMessage response = await client.PostAsync(uri, content);
                    String responseString = await response.Content.ReadAsStringAsync();
                    txtOut.Text = responseString;
                }
                catch (Exception oEx)
                {
                    txtOut.Text = "Exception: " + oEx.Message;
                }
            }



    Thursday, April 9, 2015 12:04 AM
    Moderator

All replies

  • Can you give more explicit information on what parameters are you actually trying to pass in? For example, if I use the below code, then both HTTP requests send the POST request using the same header & data:

    A.) The HTTP header: Content-Type: application/x-www-form-urlencoded

    B.) The HTTP body: "name=John+Doe&age=45&test=A%2BB%3DC" (without quotes)

    private async void btnSendRequest_Click(object sender, RoutedEventArgs e)
            {
                txtOut.Text = "";
                try
                {
                    Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient();
                    Uri uri = new Uri("http://127.0.0.1/time.aspx");
                    List<KeyValuePair<string, string>> formData = new List<KeyValuePair<string, string>>();
                    formData.Add(new KeyValuePair<string, string>("name", "John Doe"));
                    formData.Add(new KeyValuePair<string, string>("age", "45"));
                    formData.Add(new KeyValuePair<string, string>("test", "A+B=C"));
                    
                    Windows.Web.Http.HttpFormUrlEncodedContent content = new Windows.Web.Http.HttpFormUrlEncodedContent(formData);
                    Windows.Web.Http.HttpResponseMessage response = await client.PostAsync(uri, content);
                    String responseString = await response.Content.ReadAsStringAsync();
                    txtOut.Text = responseString;
                }
                catch (Exception oEx)
                {
                    txtOut.Text = "Exception: " + oEx.Message;
                }
            }
    
            private async void btnSendRequest_old_Click(object sender, RoutedEventArgs e)
            {
                txtOut.Text = "";
                try
                {
                    System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
                    Uri uri = new Uri("http://127.0.0.1/time.aspx");
                    List<KeyValuePair<string, string>> formData = new List<KeyValuePair<string, string>>();
                    formData.Add(new KeyValuePair<string, string>("name", "John Doe"));
                    formData.Add(new KeyValuePair<string, string>("age", "45"));
                    formData.Add(new KeyValuePair<string, string>("test", "A+B=C"));
    
                    System.Net.Http.FormUrlEncodedContent content = new System.Net.Http.FormUrlEncodedContent(formData);
    
                    System.Net.Http.HttpResponseMessage response = await client.PostAsync(uri, content);
                    String responseString = await response.Content.ReadAsStringAsync();
                    txtOut.Text = responseString;
                }
                catch (Exception oEx)
                {
                    txtOut.Text = "Exception: " + oEx.Message;
                }
            }



    Thursday, April 9, 2015 12:04 AM
    Moderator
  • Thanks Prashant.

    Thursday, April 9, 2015 7:27 AM