It seems that HttpClient
is the recommended way to do HTTP communication. Downloading HTML of a URL seemed easy like
var html = httpClient.GetStringAsync(url);
But I need to change some header values like Referer
each time. At first, I tried
httpClient.DefaultRequestHeaders.Add("Referer", referrer);
, but this caused an error at the second time. It seems that it is once set, cannot be changed.
I searched for a solution and found one ( https://stackoverflow.com/a/12023307/455796 ), but this seems very complicated than GetStringAsync
. I need to create a HttpRequestMessage
, call SendAsync
, continue to call response.Content.ReadAsAsync
, call Wait()
, and then read the result. Also, the comment says I need to dispose the HttpRequestMessage
. If this is the only way to change headers, I will do so, but is this the best way? Can't I use GetStringAsync
and change a header value?