2,789 questions
How to fix an invalid URI when calling client.SendAsync
Coreysan
1,811
Reputation points
I'm learning HttpClient, and have made progress, but I'm getting
An invalid request URI was provided. The request URI must either be an absolute URI or BaseAddress must be set.
Here's my code, which works really well in VS2019, but not in ASP.NET web forms. It aborts when trying to invoke SendAsync.
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", the_token);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.Timeout = TimeSpan.FromSeconds(60);
var RequestString = ConfigurationManager.AppSettings["URIForSChedules"] + "?Key=" + wkey;
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, RequestString);
HttpResponseMessage responseResult = client.SendAsync(request).Result;
int status = (int)responseResult.StatusCode;
if (status != 200) { return "-1"; }
string responseBody = responseResult.Content.ReadAsStringAsync().Result;
HttpResponseMessage response = client.GetAsync(RequestString).Result;
response.EnsureSuccessStatusCode();
I tried using an absolute URI but it just complained louder. Is there something here that should be different?
Windows development | Windows API - Win32
Sign in to answer