Questions tagged [httpcontent]
79 questions
5
votes
2 answers
Send file to service using Microsoft.Net.Http
I have a method:
private bool UploadFile(Stream fileStream, string fileName)
{
HttpContent fileStreamContent = new StreamContent(fileStream);
using (var client = new HttpClient())
{
using…

Shawn
- 2,356
- 6
- 48
- 82
4
votes
1 answer
HttpContent: An item with the same key has already been added
We have been seeing the following stack trace in our code base:
System.ArgumentException: An item with the same key has already been added.
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at…

Klaus Nji
- 18,107
- 29
- 105
- 185
4
votes
2 answers
HTTP Post Request returns 400 C#
I am trying to make a http post request to obtain an api token. If successful, it is supposed to return string values of access token, token type (bearer) and expires_in.
The code I have is a generic one which I was expecting to see working. But…

Yavar Hasanov
- 513
- 1
- 6
- 12
4
votes
2 answers
Read HttpContent stream until a character limit using StreamReader
I am trying to convert the following code that reads the complete string response of a HttpContent into a string, to read only a certain maximum number of characters. Existing code:
private static async Task…

Romonov
- 8,145
- 14
- 43
- 55
4
votes
1 answer
C# Unable to print euro symbol into a file (when opening with Excel)
I have a problem with a get method into a web api controller. This method returns a HttpResponseMessage object which has a HttpContent with a csv file, which contains euro symbols. When the method returns the file, the euro symbol isn't…

user2050468
- 41
- 1
- 3
3
votes
1 answer
How to POST Json in HttpContent and Parse it in API Controller?
I am wanting to take JSON, put it in a StringContent and pass it to my API. From there, i am wanting to deserialize the JSON back over to a class and use it from there. Currently i am getting an error for trying to deserialize which from what i can…

user1732364
- 925
- 4
- 28
- 58
2
votes
1 answer
Synchronously acquiring the Response.Content of asynchronous HttpClient.SendAsync, in .NET Standard 2.0
How can I properly extract an HttpResponseMessage Content property (of type HttpContent) in .NET Standard 2.0 plain synchronous manner, acquired after using asynchronous await HttpClient.SendAsync(request), without using the deadlock dangerous task…

cobrajohngs
- 29
- 4
2
votes
1 answer
c# httpcontent - add Header If-Match errors
adding new header "If-Match"
using (HttpContent content = new StringContent(serializedObject))
{
content.Headers.Remove("Content-Type");
content.Headers.Add("Content-Type", "application/json");
…

Samw
- 128
- 1
- 10
2
votes
3 answers
CopyToAsync vs ReadAsStreamAsync for huge request payload
I have to compute hash for huge payload, so I am using streams not to load all request content in memory. The question is what are the differences between this code:
using (var md5 = MD5.Create())
using (var stream = await…

Jevgenij Nekrasov
- 2,690
- 3
- 30
- 51
2
votes
1 answer
posting a json using httpClient is not arriving at the server
I have this code:
String json = "{\"name\":\"listFiles\"}";
// Wrap our JSON inside a StringContent which then can be used by the HttpClient class
HttpContent httpContent = new StringContent(json, Encoding.UTF8,…

mans
- 17,104
- 45
- 172
- 321
2
votes
2 answers
How to pass the File Data from C# Console application to WebApi?
I am trying to call the Web api method for saving the File Data.When I debug Webapi method I found that ContentLength is not coming as correct, because of this when i am retrieving the file it is showing error as corrupted file.
My Class method is…

Gorakh Nath
- 9,140
- 15
- 46
- 68
2
votes
1 answer
Make a HttpClient.PutAsync request with IHttpContent
How can I create an IHttpContent object with content to PutAsync request body?
The request is like this:
IHttpContent myHttpContent = ???
var response = await httpClient.PutAsync(uri, myHttpContent);
I'm using the Windows.Web.HttpClient library.

Schrödinger's Box
- 3,218
- 1
- 30
- 51
1
vote
1 answer
C# - httpclient - json body - Not getting applied appropriately using httpcontent
The following is the code from where I would return a tuple of response status code and response output.
private Tuple API_Check(string URL, string reqtype, string reqbody, string split_username, string split_pwd)
{
string…

ItsMeGokul
- 403
- 1
- 5
- 16
1
vote
1 answer
How to retrieve JSON data from HttpContent
I'm buildin a console Web API to communicate with a localhost server, hosting computer games and highscores for them. Every time I run my code, I get this charming error:
fail:
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
An…

theodorn
- 149
- 8
1
vote
1 answer
Problems downloading web pages
In one controller (MVC ASP.NET) I need to download some web pages, and analyze them.
Currently I'm using Visual Studio 2019, and .NET Framework 4.8.
My code is like this (simplified):
public async void GetHtmlStream(Uri urlAddr)
{
HttpClient…

Giorgio Forti
- 131
- 2
- 14