CoreyFleig-6304 avatar image
0 Votes"
CoreyFleig-6304 asked

How to fix an invalid URI when calling client.SendAsync

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;

I tried using an absolute URI but it just complained louder. Is there something here that should be different?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

0 Answers