37

因此,我创建了一个HttpClient并正在使用HttpClient.PostAsync().

我设置HttpContent使用

HttpContent content = new FormUrlEncodedContent(post_parameters); 哪里post_parameters是键值对列表List<KeyValuePair<string, string>>

问题是,当HttpContent值很大时(将图像转换为 base64 进行传输),我得到一个 URL 太长的错误。这是有道理的——因为网址不能超过 32,000 个字符。HttpContent但是如果不是这种方式,我该如何将数据添加到其中?

请帮忙。

4

3 回答 3

65

我在朋友的帮助下想通了。您想要做的是避免使用 FormUrlEncodedContent(),因为它对 uri 的大小有限制。相反,您可以执行以下操作:

    var jsonString = JsonConvert.SerializeObject(post_parameters);
    var content = new StringContent(jsonString, Encoding.UTF8, "application/json");

在这里,我们不需要使用 HttpContent 来发布到服务器,StringContent 完成了工作!

于 2014-05-23T17:36:17.513 回答
31

FormUrlEncodedContent内部使用Uri.EscapeDataString:从反射中,我可以看到该方法具有限制请求长度大小的常量。

一个可能的解决方案是FormUrlEncodedContent通过使用System.Net.WebUtility.UrlEncode(.net 4.5)创建一个新的实现来绕过这个限制。

public class MyFormUrlEncodedContent : ByteArrayContent
{
    public MyFormUrlEncodedContent(IEnumerable<KeyValuePair<string, string>> nameValueCollection)
        : base(MyFormUrlEncodedContent.GetContentByteArray(nameValueCollection))
    {
        base.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
    }
    private static byte[] GetContentByteArray(IEnumerable<KeyValuePair<string, string>> nameValueCollection)
    {
        if (nameValueCollection == null)
        {
            throw new ArgumentNullException("nameValueCollection");
        }
        StringBuilder stringBuilder = new StringBuilder();
        foreach (KeyValuePair<string, string> current in nameValueCollection)
        {
            if (stringBuilder.Length > 0)
            {
                stringBuilder.Append('&');
            }

            stringBuilder.Append(MyFormUrlEncodedContent.Encode(current.Key));
            stringBuilder.Append('=');
            stringBuilder.Append(MyFormUrlEncodedContent.Encode(current.Value));
        }
        return Encoding.Default.GetBytes(stringBuilder.ToString());
    }
    private static string Encode(string data)
    {
        if (string.IsNullOrEmpty(data))
        {
            return string.Empty;
        }
        return System.Net.WebUtility.UrlEncode(data).Replace("%20", "+");
    }
}

要发送大型内容,最好使用StreamContent

于 2014-05-19T14:27:21.827 回答
4

这段代码对我有用,基本上你通过http客户端在字符串内容中发送post数据“application/x-www-form-urlencoded”,希望这可以帮助像我一样有同样问题的人

void sendDocument()
    {
        string url = "www.mysite.com/page.php";
        StringBuilder postData = new StringBuilder();
        postData.Append(String.Format("{0}={1}&", HttpUtility.HtmlEncode("prop"), HttpUtility.HtmlEncode("value")));
        postData.Append(String.Format("{0}={1}", HttpUtility.HtmlEncode("prop2"), HttpUtility.HtmlEncode("value2")));
        StringContent myStringContent = new StringContent(postData.ToString(), Encoding.UTF8, "application/x-www-form-urlencoded");
        HttpClient client = new HttpClient();
        HttpResponseMessage message = client.PostAsync(url, myStringContent).GetAwaiter().GetResult();
        string responseContent = message.Content.ReadAsStringAsync().GetAwaiter().GetResult();
    }
于 2018-01-17T18:19:46.807 回答