dotnet-httpclient

System.Net.Http.HttpClient.PostAsync blocks and never returns

陌路散爱 提交于 2020-01-24 17:04:11
问题 I have a .NET framework Windows Forms application with a form that has this code: using System; using System.Collections.Generic; using System.IO; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace test { public partial class Main : Form { public int exitCode = 1; private Options opts; CancellationTokenSource cancellationSource = new CancellationTokenSource(); public Main(Options opts) { InitializeComponent(); this.opts = opts; }

Httpclient This instance has already started one or more requests. Properties can only be modified before sending the first request

无人久伴 提交于 2020-01-21 04:17:07
问题 I am creating an application in .Net Core 2.1 and I am using http client for web requests. The issue is I have to send parallel calls to save time and for that I am using Task.WhenAll() method but when I hit this method I get the error "This instance has already started one or more requests. Properties can only be modified before sending the first request" Previously I was using RestSharp and everything was fine but I want to use httpclient. Here is the code: public async Task<User> AddUser

How to handle Http Status code when making use of ReadAsStringAsync()

删除回忆录丶 提交于 2020-01-17 01:45:12
问题 I have a JavaScript client which makes Ajax call to a .net service (Lets call it First service). First service then makes call to another .net Controller (Call it Second Service). In this controller, I am throwing some exception. On the first line I am saying: //Code from Second Service [HttpPost] public HttpResponseMessage Results(ParamsModel data) { throw new Exception("Exception for testing purpose"); } //Code from First Service [HttpPost] public ActionResult Results(ParamsModel data) {

InvalidOperationException on a PostAsXmlAsync call

*爱你&永不变心* 提交于 2020-01-14 14:08:09
问题 When calling HttpClient method PostAsXmlAsync, I'm getting the following InvalidOperationException: Token EndElement in state End Document would result in an invalid XML document. Why am I getting this exception? var user = new RXGRequestUser(); var client = new HttpClient(); var response = await client.PostAsXmlAsync(@"myurl.com", user); RXGRequestUser: public class RXGRequestUser : IXmlSerializable { public string Username { get; set; } public string Password { get; set; } public string

Disable AutoRedirect in FlurlClient

依然范特西╮ 提交于 2020-01-13 09:39:12
问题 I am using FlurlHttp and I want to disable AllowAutoRedirect for some API calls. I know How can I get System.Net.Http.HttpClient to not follow 302 redirects? WebRequestHandler webRequestHandler = new WebRequestHandler(); webRequestHandler.AllowAutoRedirect = false; HttpClient httpClient = new HttpClient(webRequestHandler); // Send a request using GetAsync or PostAsync Task<HttpResponseMessage> response = httpClient.GetAsync("http://www.google.com") But for Flurl I found only the way similar

How do I set a response cookie on HttpReponseMessage?

…衆ロ難τιáo~ 提交于 2020-01-12 06:31:25
问题 I would like to create a demo login service in web api and need to set a cookie on the response. How do I do that? Or are there any better way to do authorization? 回答1: Add a reference to System.Net.Http.Formatting.dll and use the AddCookies extension method defined in the HttpResponseHeadersExtensions class. Here is a blog post describing this approach, and the MSDN topic. If that assembly isn't an option for you, here's my older answer from before this was an option: Older answer follows I

HttpClient fails to authenticate via NTLM on the second request when using the Sharepoint REST API on Windows Phone 8.1

谁都会走 提交于 2020-01-11 12:22:51
问题 Sorry for the long title, but it seems to be the best summary based on what I know so far. We’re currently working on a Universal App that needs to access some documents on a Sharepoint server via the REST API using NTLM Authentication, which proves to be more difficult than it should be. While we were able to find workarounds for all problems (see below), I don’t really understand what is happening and why they are even necessary. Somehow the HttpClient class seems to behave differently on

HttpClient ReadAsAsync<Type> only deserializing part of the response

谁说我不能喝 提交于 2020-01-07 03:27:09
问题 I am using the following to make a call to a WebAPI using (HttpClient client = HttpClientFactory.Create(new AuthorisationHandler())) { client.BaseAddress = new Uri(BaseURI); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/xml")); var httpResponseMessage = await client.PostAsXmlAsync<AvailRequest>("Avail/Search/", req); httpResponseMessage.EnsureSuccessStatusCode(); var availResp =

Upload progress using HttpClient in Blazor Wasm

扶醉桌前 提交于 2020-01-06 08:16:51
问题 I'm trying to get file upload progress inside a blazor web assembly app. var handler = new ProgressMessageHandler(); handler.InnerHandler = new WebAssemblyHttpMessageHandler(); handler.HttpSendProgress += (object sender, HttpProgressEventArgs e) => { Console.WriteLine("bytes transfered= " + e.BytesTransferred.ToString()); Console.WriteLine("total bytes= " + e.TotalBytes.ToString()); Console.WriteLine("progress percentage= " + e.ProgressPercentage.ToString()); }; var http = new HttpClient

Post (or put) request with uri parameter not working when calling with HttpClient

依然范特西╮ 提交于 2020-01-06 08:04:55
问题 In trying to make a Web API in c# and consume it via c# HttpClient, I've already written some controllers and client calls and is working fine, but now I need to call a Post method with two parameters, one simple value (long) directly in the URL[FromURI], and the another one is a complex type passed in body. I think I can simplify the answer, suppose I just want to call a post or put with one simple parameter passed in the URL. First of all, lets see my web server mapping config: