How to fix an invalid URI when calling client.SendAsync

Coreysan 1,811 Reputation points
2021-06-16T00:33:00.61+00:00

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
0 comments No comments
{count} vote

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.