Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

c# - How to set large string inside HttpContent when using HttpClient?

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

I set the HttpContent using

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

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?

Please help.

question from:https://stackoverflow.com/questions/23703735/how-to-set-large-string-inside-httpcontent-when-using-httpclient

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...