C#: HttpClient with POST parameters

A cleaner alternative would be to use a Dictionary to handle parameters. They are key-value pairs after all. private static readonly HttpClient httpclient; static MyClassName() { // HttpClient is intended to be instantiated once and re-used throughout the life of an application. // Instantiating an HttpClient class for every request will exhaust the number of … Read more

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

HttpClient.DefaultRequestHeaders (and BaseAddress) should only be set once, before you make any requests. HttpClient is only safe to use as a singleton if you don’t modify it once it’s in use. Rather than setting DefaultRequestHeaders, set the headers on each HttpRequestMessage you are sending. var request = new HttpRequestMessage(HttpMethod.Post, url); request.Headers.Accept.Clear(); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(“application/json”)); request.Headers.Authorization = … Read more

How to use HttpClient to Post with Authentication

First you have to set the Authorization-Header with your <clientid> and <clientsecret>. Instead of using StringContent you should use FormUrlEncodedContent as shown below: var client = new HttpClient(); client.BaseAddress = new Uri(“http://myserver”); var request = new HttpRequestMessage(HttpMethod.Post, “/path”); var byteArray = new UTF8Encoding().GetBytes(“<clientid>:<clientsecret>”); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(“Basic”, Convert.ToBase64String(byteArray)); var formData = new List<KeyValuePair<string, string>>(); formData.Add(new … Read more

C# Add Accept header to HttpClient

There is no difference. DefaultRequestHeaders.Accept is a collection of string type, where you can add your header to accept using the new instance of MediaTypeWithQualityHeaderValue. client.DefaultRequestHeaders is a dictionary that accepts key for and value for the request header and matches the results according to them. DefaultRequestHeaders has overloads. The only thing that differs between … Read more

Using HttpClient, how would I prevent automatic redirects and get original status code and forwading Url in the case of 301 [duplicate]

I have found that the way to do this is by creating an instance of HttpClientHandler and passing it in the constructor of HttpClient public static async void makeRequest(int row, string url) { string result; Stopwatch sw = new Stopwatch(); sw.Start(); // added here HttpClientHandler httpClientHandler = new HttpClientHandler(); httpClientHandler.AllowAutoRedirect = false; try { // … Read more

Ruby – net/http – following redirects

To follow redirects, you can do something like this (taken from ruby-doc) Following Redirection require ‘net/http’ require ‘uri’ def fetch(uri_str, limit = 10) # You should choose better exception. raise ArgumentError, ‘HTTP redirect too deep’ if limit == 0 url = URI.parse(uri_str) req = Net::HTTP::Get.new(url.path, { ‘User-Agent’ => ‘Mozilla/5.0 (etc…)’ }) response = Net::HTTP.start(url.host, url.port, … Read more

HttpClient vs HttpWebRequest for better performance, security and less connections

If you use either of them with async it should be good for the performance point of view as it will not block the resources waiting for the response and you will get good throughput. HttpClient is preferred over HttpWebRequest due to async methods available out of the box and you would not have to … Read more

How to use restsharp to download file

With RestSharp, it’s right there in the readme: var client = new RestClient(“http://example.com”); client.DownloadData(request).SaveAs(path); With HttpClient, it’s a bit more involved. Have a look at this blog post. Another option is Flurl.Http (disclaimer: I’m the author). It uses HttpClient under the hood and provides a fluent interface and lots of convenient helper methods, including: await … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)