StringContent Vs FormUrlEncodedContent

I have a URL. I want to publish a body with parameters for such data as data = "blahblahblah". However, my "blahblahblah" in this case is full XML, I just ask him to do something like below:

<Parent id="1"> <Child id="1"/> </Parent> 

I can get this to work to find with HTTPClient FormUrlEncodedContent find with the following approach.

  var values = new List<KeyValuePair<string, string>>(); values.Add(new KeyValuePair<string, string>("data", XMLBody)); var content = new FormUrlEncodedContent(values); HttpResponseMessage sResponse = await sClient.PostAsync(action.URL, content).ConfigureAwait(false); 

Now I want this to work with StringContent. Basically sending xml as part of the parameter value, but that xml contains "=" . The code below does not work, since I can publish it, but the server does not recognize the xml data. Am I something wrong here?

 StringContent content = new StringContent(HttpUtility.UrlEncode(action.Body), Encoding.UTF8, "application/x-www-form-urlencoded"); HttpResponseMessage sResponse = await sClient.PostAsync(action.URL, content ).ConfigureAwait(false); 
+6
source share
1 answer

I found it, I need to manually enter the data = part.

 StringContent content = new StringContent("data="+ HttpUtility.UrlEncode(action.Body), Encoding.UTF8, "application/x-www-form-urlencoded"); HttpResponseMessage sResponse = await sClient.PostAsync(action.URL, content ).ConfigureAwait(false); 
+7
source

Source: https://habr.com/ru/post/979798/


All Articles