Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to return string, but in my visual studio it highlight as error the return values, when i read the error it say " Convert type 'string ' to 'system.net.http.HttpResponseMessage". I dont know the reason of the error!, the code is below, please help me thanks.

What I have tried:

public static async Task<string> GetByLastDate(string specificUrl, string token, DateTime date) // specificUrl like "user/" at the end "/"
{
var policy = Policy
  .Handle<Exception>()
  .OrResult<HttpResponseMessage>(r => !r.IsSuccessStatusCode)
  .RetryAsync(2);

   return await policy.ExecuteAsync(async() =>
   {
    string fullUrl = baseUrl + specificUrl + date;
      using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, fullUrl))
       {
         requestMessage.Headers.Add("access_token", token);
         using (HttpResponseMessage res = await client.SendAsync(requestMessage))
          {
            string data = await res.Content.ReadAsStringAsync();
             if (string.IsNullOrEmpty(data)) { return string.Empty; }
               return data;
             }

         }
   }).ConfigureAwait(false);

}
Posted
Comments
CHill60 7-Sep-21 8:16am    
Some advice on how to get an answer to your question
1. Tell us which line produces this error
2. Give us the full text of the error
Member 14921707 7-Sep-21 8:37am    
return string.Empty;
return data;
this two are highlighted, error of "Convert type 'string ' to 'system.net.http.httpresponsemessage'"
Member 14921707 7-Sep-21 8:45am    
var policy = Policy
.Handle<exception>()
.OrResult<httpresponsemessage>(r => !r.IsSuccessStatusCode)
.RetryAsync(2);

string fullUrl = baseUrl + specificUrl+ date;
using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, fullUrl))
{
requestMessage.Headers.Add("access_token", token);
using (HttpResponseMessage res = await policy.ExecuteAsync(() => client.SendAsync(requestMessage)))
{
string data = await res.Content.ReadAsStringAsync();
if (string.IsNullOrEmpty(data)) { return string.Empty; }
return data;
}

}
This is working but when the second try, it says "The request message was already send, cant send same request multiple time! "
that is why i make await policy.ExcuteAsync at the first line
Member 11091164 19-Oct-22 23:37pm    
Did you get the answer to your question? I face the same problem in return type

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900