如何使用HttpClient的时设置里面HttpContent大字符串? [英] How to set large string inside HttpContent when using HttpClient?

查看:1709
本文介绍了如何使用HttpClient的时设置里面HttpContent大字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我创建了一个的HttpClient 和正在使用的数据发布 HttpClient.PostAsync()

So, I created a HttpClient and am posting data using HttpClient.PostAsync().

我设置了​​ HttpContent 使用

HttpContent内容=新FormUrlEn codedContent(post_parameters);其中, post_parameters 是键值对的列表列表< KeyValuePair<字符串,字符串>>

HttpContent content = new FormUrlEncodedContent(post_parameters); where post_parameters is a list of Key value pairs List<KeyValuePair<string, string>>

问题是,当 HttpContent 有一个较大的值(图像转换为base64要传输),我得到一个网址太长错误。这是有道理的 - 导致URL着超越32,000个字符。但我怎么将数据添加到 HttpContent 如果不是这样?

Problem is, when the HttpContent has a large value (an image converted to base64 to be transmitted) I get a URL is too long error. That makes sense - cause the url cant go beyond 32,000 characters. But how do I add the data into the HttpContent if not this way?

请帮忙。

推荐答案

我和我的朋友的帮助下了它。你会想要做的是避免使用FormUrlEn codedContent(),因为它的URI的大小限制。相反,你可以做到以下几点:

I figured it out with the help of my friend. What you would want to do is avoid using FormUrlEncodedContent(), because it has restrictions on the size of the uri. Instead, you can do the following :

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

在这里,我们并不需要使用HttpContent张贴到服务器的StringContent就完事了!

Here, we don't need to use HttpContent to post to the server, StringContent gets the job done !

这篇关于如何使用HttpClient的时设置里面HttpContent大字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆