StringContent与FormUrlEncodedContent

Fyl*_*lix 13 c# http-post

我有一个URL我希望以data ="blahblahblah"的形式发布带有参数的主体.但是,在这种情况下,我的"blahblahblah"是一个完整的XML,我将其简化为如下所示:

      <Parent id="1">
         <Child id="1"/>
      </Parent>
Run Code Online (Sandbox Code Playgroud)

我可以通过以下方法找到HTTPClient FormUrlEncodedContent查找工作.

        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);
Run Code Online (Sandbox Code Playgroud)

现在我想让它与StringContent一起使用. 基本上发送xml作为参数值的一部分,并且xml包含"=".下面的代码不起作用,因为我可以发布它,但服务器无法识别xml数据.我在这里做错了吗?

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);
Run Code Online (Sandbox Code Playgroud)

Fyl*_*lix 17

我找到了,我必须手动输入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);
Run Code Online (Sandbox Code Playgroud)