dotnet-httpclient

Is it fine to use one HttpClient instance for each host my application needs to talk to?

邮差的信 提交于 2019-12-22 10:58:55
问题 I know that, when using the Microsoft dependency injection container, the best practice to handle HttpClient instances is using the IHttpClientFactory interface provided by the Microsoft.Extensions.Http nuget package. Unfortunately the classes implementing the IHttpClientFactory interface are not public (as you can verify here), so the only way to exploit this pattern is using the Microsoft dependency injection container (at least it's the only one that I know). Sometimes I need to maintain

System.Net.Http missing?

放肆的年华 提交于 2019-12-22 10:37:15
问题 I am trying to run Test.aspx: <%@ Page language="c#" EnableViewState="true" ContentType="text/html" Async="true" %> <script language="C#" runat="server"> void Page_Load(Object Src, EventArgs E ) { RegisterAsyncTask(new PageAsyncTask(BindData)); } private async System.Threading.Tasks.Task BindData() { Response.Write("Hello?<br /><br />"); using (System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient()) { Response.Write(await httpClient.GetStringAsync("http://www.google.com"));

What is the purpose of HttpClient.BaseAddress and why can't I change it after the first request

99封情书 提交于 2019-12-22 09:45:24
问题 So most of us have probably read that we are supposed to reuse instances of HttpClient instead of using using and creating new ones. That means I can just create a single instance of HttpClient in my program and call GetAsync using a full uri string per request. This leads me to the BaseAddress property of HttpClient . Consider the following code: HttpClient microsoftClient = new HttpClient() { BaseAddress = new Uri("https://www.microsoft.com/") }; HttpClient stackoverflowClient = new

Having dynamic proxy with HttpClientFactory implementation

不打扰是莪最后的温柔 提交于 2019-12-22 07:56:10
问题 I have Asp.Net Core WebApi . I am making Http requests according to HttpClientFactory pattern. Here is my sample code: public void ConfigureServices(IServiceCollection services) { ... services.AddHttpClient<IMyInterface, MyService>(); ... } public class MyService: IMyInterface { private readonly HttpClient _client; public MyService(HttpClient client) { _client = client; } public async Task CallHttpEndpoint() { var request = new HttpRequestMessage(HttpMethod.Get, "www.customUrl.com"); var

Equivalent of “curl -F” parameter for System.Net.Http.MultipartFormDataContent?

百般思念 提交于 2019-12-22 07:56:08
问题 I am trying to use the sonicAPI file/upload API in C#. My attempt to translate the curl example to C# with HttpClient and MultipartFormDataContent returns the error 400 / Bad Request . Content of the response : <?xml version="1.0" encoding="UTF-8"?> <response> <status code="400" /> <errors> <error message="File upload failed: file is missing." parameter="file" error_code="10" /> </errors> </response> Example of the curl command-line shown in the documentation : curl https://api.sonicapi.com

View Raw Headers that will be sent/received in HttpResponseMessage / HttpRequestMessage (System.Net.Http, WebAPI)

痴心易碎 提交于 2019-12-22 07:00:09
问题 It can be extremely beneficial to visually see the raw list of Http Headers that will actually be sent or received in WebAPI's HttpResponseMessage / HttpRequestMessage types. I mean just a plain old string, with each header on a new line, exactly what is generated or received. But unfortunately, it does not look like either of these types allows you to just see what actually gets generated. Instead, there are properties scattered everywhere. Some in the raw HttpResponseMessage /

How to call ASP .NET MVC WebAPI 2 method properly

北城余情 提交于 2019-12-22 06:36:23
问题 I have created separated project for ASP .NET MVC WebAPI 2 and I would like to call Register method. (I use a project is created by VS2013 by default.) [Authorize] [RoutePrefix("api/Account")] public class AccountController : ApiController { .... // POST api/Account/Register [AllowAnonymous] [Route("Register")] public async Task<IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return BadRequest(ModelState); } IdentityUser user = new IdentityUser { UserName

HttpClient PostAsync posting null content

99封情书 提交于 2019-12-22 05:42:05
问题 I have one api calling another. and here is my code which seems to cause ModelState.IsValid = false on the other side of world. var baseUri = new Uri("http://localhost:5001/"): _httpClient.BaseAdress = baseUri; var data = new StringContent(content: model.Tostring(), encoding: Encoding.UTF8, mediaType: "application/json"); var response = await _httpClient.PostAsync("api/product", data); watching Post([FromBody]Product product) on the api being called I just see product=null . changing to Post(

How to call PUT method from Web Api using HttpClient?

僤鯓⒐⒋嵵緔 提交于 2019-12-22 05:27:27
问题 I want to call Api function (1st) . from 2nd Api function using HttpClient . But I always get 404 Error. 1st Api Function ( EndPoint : http : // localhost : xxxxx /api/Test/) public HttpResponseMessage Put(int id, int accountId, byte[] content) [...] 2nd Api function public HttpResponseMessage Put(int id, int aid, byte[] filecontent) { WebRequestHandler handler = new WebRequestHandler() { AllowAutoRedirect = false, UseProxy = false }; using (HttpClient client = new HttpClient(handler)) {

C#: HttpClient, The server committed a protocol violation. Section=ResponseStatusLine

我与影子孤独终老i 提交于 2019-12-22 03:49:21
问题 I'm using the HttpClient class to communicate to a web service in my WPF application. When I make consecutive GET requests on the same connection, everything works fine. However, when I make consecutive PUT/PATCH requests on the same connection, the first request executes accurately and I receive a response but the second request does not include the body in the request and I receive the infamous error of "The server committed a protocol violation. Section=ResponseStatusLine". My requests do