httpcontent

Read HttpContent stream until a character limit using StreamReader

◇◆丶佛笑我妖孽 提交于 2020-04-13 06:33:09
问题 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<string> GetContentStringAsync(HttpContent content) { string responseContent = await content.ReadAsStringAsync().ConfigureAwait(false); return responseContent; } Code that I have now: private static async Task<string> GetContentStringAsync(HttpContent content, int ResponseContentMaxLength)

Retrying HttpClient Unsuccessful Requests

╄→尐↘猪︶ㄣ 提交于 2019-12-27 11:37:07
问题 I am building a function that given an HttpContent Object, will issues request and retry on failure. However I get exceptions saying that HttpContent Object is disposed after issuing the request. Is there anyway to copy or duplicate the HttpContent Object so that I can issue multiple requests. public HttpResponseMessage ExecuteWithRetry(string url, HttpContent content) { HttpResponseMessage result = null; bool success = false; do { using (var client = new HttpClient()) { result = client

HttpResponseMessage.Content.Header ignoring charset setting in meta tag in html source

时光毁灭记忆、已成空白 提交于 2019-12-25 08:48:33
问题 I have just posted this question, which answer came right away. It, in turn, creates the following new question: If my understanding is correct, the StreamContent object, from HttpResponseMessage , is created upon making an HTTP request via HttpClient.GetAsync . Its Header property, or part of it, will be set according to meta tags included in the HTML source file. For instance, a meta tag can tell the response object with which charset encode the file's contents. <meta http-equiv='Content

localhost route not working should be returning JSON back when locating to URL

有些话、适合烂在心里 提交于 2019-12-24 19:03:54
问题 My fellow programmers, I basically have this async Get() method which is reading Json data successfully, but when locating to route -> localhost:59185/api/encompass/data I receive a message: No HTTP resource was found that matches the request URL 'http://localhost:59185/api/encompass/data'. </Message> I was very hopeful that it would return my JSON especially when in debug the code its sitting in 'string res' at the bottom anyone know why its not returning Json even thought its sitting in

C# Unable to print euro symbol into a file (when opening with Excel)

我的梦境 提交于 2019-12-23 12:27:02
问题 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 printed. The code of the method is the following: string export = ... //string with fields separed by ';' and with euro symbol HttpResponseMessage response = new HttpResponseMessage(); UTF8Encoding encoding = new UTF8Encoding(); Byte[] buffer = encoding

Post JSON HttpContent to ASP.NET Web API

混江龙づ霸主 提交于 2019-12-20 19:58:08
问题 I have an ASP.NET Web API hosted and can access http get requests just fine, I now need to pass a couple of parameters to a PostAsync request like so: var param = Newtonsoft.Json.JsonConvert.SerializeObject(new { id=_id, code = _code }); HttpContent contentPost = new StringContent(param, Encoding.UTF8, "application/json"); var response = client.PostAsync(string.Format("api/inventory/getinventorybylocationidandcode"), contentPost).Result; This call is returning a 404 Not Found result. The

HttpContent.ReadAsStringAsync causes request to hang (or other strange behaviours)

偶尔善良 提交于 2019-12-10 03:22:22
问题 We are building a highly concurrent web application, and recently we have started using asynchronous programming extensively (using TPL and async / await ). We have a distributed environment, in which apps communicate with each other through REST APIs (built on top of ASP.NET Web API). In one specific app, we have a DelegatingHandler that after calling base.SendAsync (i.e., after calculating the response) logs the response to a file. We include the response's basic information in the log

How do I send arbitrary JSON data, with a custom header, to a REST server?

巧了我就是萌 提交于 2019-12-09 17:01:10
问题 TL;DR -- How do I send a JSON string to a REST host with an auth header? I've tried 3 different approaches found one that works with anonymous types. Why can't I use anonymous types? I need to set a variable called "Group-Name", and a hyphen isn't a valid C# identifier. Background I need to POST JSON but am unable to get the body and the content type correct Function #1 - Works with anonymous types The content type and data is correct, but I don't want to use anonymous types. I want to use a