dotnet-httpclient

What's the difference between a content header and a header?

夙愿已清 提交于 2019-12-04 04:44:18
问题 HttpRequestMessage Has Content.Headers and Headers Why is that? when I google Http protocol online, I don't see anyone mentioning a content header and a normal header, there are only "headers" 回答1: Unfortunately, the MSDN documentation for MVC, WebAPI and System.Net.Http are appaling. See HttpContentHeaders.ContentType Property: Gets or sets {insert text here}. Well-known content-specific headers are grouped under "content headers". It's just for convenience. There are headers like content

Using HttpClient in a Xamarin core project

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 04:04:52
I am doing a project for multiple platforms where I have followed best practices and created a core project to contain all the code that is the same on all platforms. To do the network communication I would like to use HttpClient which seems to be the recommended approach for communicating with web services going forward. I am NOT doing a PCL but just a standard C# library to contain the core project. However it seems that there is no common implementation of HttpClient but only platform specific ones. I cannot reference and use it in my core but only in my Android and iOS projects. I would

Object is disposed after PostAsync with HttpClient

回眸只為那壹抹淺笑 提交于 2019-12-04 04:04:40
I'm trying to send a file with HttpClient and if something on the receiving side fails I want to resend the same file stream. I'm creating a post request with a MultipartFormDataContent, which contains the stream. Everything looks fine when I call PostAsync for the first time. But when I try to repeat the request I get System.ObjectDisposedException. My file stream is disposed after the first call of PostAsync... Why and is there a solution to my problem? Here is basic example of what am I talking about. public ActionResult Index() { var client = new HttpClient { BaseAddress = new Uri(Request

Is it safe to use HttpClientFactory?

蹲街弑〆低调 提交于 2019-12-04 03:19:07
问题 In my asp.net core MVC application, I'm using HttpClientFactory to create HttpClient object for requests to API server. Follows Microsoft document, HttpClient object is created new for each time I call HttpClientFactory.CreateClient() , so it will be safe for setting values to HttpClient.DefaultRequestHeaders . About HttpMessageHandler objects, because they are pooled and can be re-used later. So, if they hold cookies information (For example: setting cookies to HttpClientHandler object), we

RestSharp ignoring system proxy (for example Fiddler) on .NET Core

纵饮孤独 提交于 2019-12-04 03:15:37
问题 I want check the http traffic with Fiddler, but no any http traffic captured, my testing codes: private static void ByRestSharp() { var restClient = new RestClient("https://jsonplaceholder.typicode.com"); var request = new RestRequest("posts", Method.GET); var response = restClient.Get<List<Post>>(request); Console.WriteLine("{0} posts return by RestSharp.", response.Data.Count); } But after I changed to use HttpClient, Fiddler can capture the http traffic, sample codes: private static void

Is my implementaton of HttpClient singleton appropriate? [closed]

别来无恙 提交于 2019-12-04 02:50:30
Closed . This question is opinion-based. It is not currently accepting answers. Learn more . Want to improve this question? Update the question so it can be answered with facts and citations by editing this post . Closed 5 years ago . I am using System.Net.HttpClient (.Net 4.5) in my MVC4 project. I know that a single instance of HttpClient can be used for all Http requests across the web app. Here is my implementation of the HttpClient singleton which should return the single instance every time. public sealed class HttpClientInstance: HttpClient { // singleton instance private static

C# async tasks waiting indefinitely

大城市里の小女人 提交于 2019-12-04 02:11:10
I am trying to use the functionality provided by "async" & "await" to asynchronously download webpage content and I have into issues where the Tasks are waiting forever to complete. Could you please let me know what is wrong with the following code snippet? protected void Page_Load(object sender, EventArgs e) { var websites = new string[] {"http://www.cnn.com","http://www.foxnews.com"}; var tasks = websites.Select(GenerateSomeContent).ToList(); //I don't want to use 'await Tasks.WhenAll(tasks)' as I need to block the //page load until the all the webpage contents are downloaded Task.WhenAll

How to use HttpClientHandler with HttpClientFactory in .NET Core

房东的猫 提交于 2019-12-04 01:31:26
I want to use the HttpClientFactory that is available in .NET Core 2.1 but I also want to use the HttpClientHandler to utilize the AutomaticDecompression property when creating HttpClients . I am struggling because the .AddHttpMessageHandler<> takes a DelegatingHandler not a HttpClientHandler . Does anyone know how to get this to work? Thanks, Jim Actually I'm not using automatic decompression but the way to achieve this is to properly register http client services.AddHttpClient<MyCustomHttpClient>() .ConfigureHttpMessageHandlerBuilder((c) => new HttpClientHandler() { AutomaticDecompression =

How to use Fiddler with HttpClient?

点点圈 提交于 2019-12-04 00:10:50
问题 I know there are many of questions/answers, blogs about this, not talking about Telerik's FAQ. Still I could not find this diagnosed and solved in a clear pure way: Context: I have a Web API app, and I have a (unit test) client, which uses HttpClient to send requests to the API. Web API app listens in http://localhost:8631/ Sometimes I use Fiddler to see what's going on. Issue: Traffic between my HttpClient and Web API is not captured by Fiddler. After launching Fiddler traffic is still OK,

C# HttpClient An existing connection was forcibly closed by the remote host

╄→尐↘猪︶ㄣ 提交于 2019-12-03 23:29:10
I'm working on an integration with Alternative Payments using their hosted page integration . Their C# SDK does not have this integration available at the moment, but as you can see it's pretty simple and I made a small class to send the post request and get the JSON response. I tested the json object I'm sending on PostMan and cURL and both work, also the authentication header, so I think they are not the problem. Here is the constructor of my class: public AlternativePaymentsCli(string apiSecretKey) { this._apiSecretKey = apiSecretKey; _httpClient = new HttpClient(); _httpClient