【问题标题】:StringContent Vs FormUrlEncodedContentStringContent 与 FormUrlEncodedContent
【发布时间】:2014-12-17 17:30:32
【问题描述】:

我有一个 URL,我想以 data="blahblahblah" 的形式发布带有参数的正文。但是,在这种情况下,我的“blahblahblah”是一个完整的 XML,我将其简化为如下所示:

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

我可以使用以下方法使其与 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);

现在我想让它与 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);

【问题讨论】:

    标签: c# http-post


    【解决方案1】:

    我找到了,我必须手动放入 data= 部分。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多