Cannot convert from 'string' to 'System.Net.Http.HttpRequestMessage'

10,655

Solution 1

You can also use the HttpClient.GetAsync or HttpClient.PostAsync depending on the nature of the request being made. Those methods do have overloads that take string URIs.

For example

HttpClient client = new HttpClient();
public async Task<string> GetResponseString() {
    var request = "http://localhost:51843/api/values/getMessage?id=1";
    var response = await client.GetAsync(request);
    var content = await response.Content.ReadAsStringAsync();
    return content;
}

Here is the synchronous version of the same method

HttpClient client = new HttpClient();
public string GetResponseString() {
    var request = "http://localhost:51843/api/values/getMessage?id=1";
    var response = client.GetAsync(request).Result;
    var content = response.Content.ReadAsStringAsync().Result;
    return content;
}

Solution 2

SendAsync has a parameter of HttpRequestMessage, and you are passing a string. That's exactly what the error is telling you.

If you lookup HttpRequestMessage, it requires a method and a URI

var url = "http://localhost:51843/api/values/getMessage?id=1";
var req = new HttpRequestMessage(HttpMethod.POST, new Uri(url));
Share:
10,655
Frank Mascia
Author by

Frank Mascia

Updated on December 03, 2022

Comments

  • Frank Mascia
    Frank Mascia over 1 year

    I'm really new to c# and making API requests in general, so not sure if my code is even making sense. Right now Im just trying to take the data I get from my API's URL call and display it onto a TextView called testText. I'm open to using either RestSharp or the HttpClient, whatever I can get to work.

    public class MainActivity : Activity
    {
    
        HttpClient client;
    
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
        }
    
        public async Task<string> GetResponseString()
        {
            var request = "http://localhost:51843/api/values/getMessage?id=1";
            var response = await client.SendAsync(request);
            var content = await response.Content.ReadAsStringAsync();
            return content;
        }
    }
    

    Inside my public async Task<> method is where I am getting the

    Cannot convert from 'string' to 'System.Net.Http.HttpRequestMessage'

    error for the request variable. I left in the comments of all my other attempts, let me know if i should try any of those

    • Jason
      Jason almost 7 years
      please strip out all of the commented code that is not needed to illustrate your problem, it just makes your question more difficult to read
    • Frank Mascia
      Frank Mascia almost 7 years
      Done, sorry was keeping those in incase someone thought those were a better route to go.
  • Frank Mascia
    Frank Mascia almost 7 years
    Both yours and Jasons solutions work, but now I'm having trouble when I called the method GetResponseString(); set it equal to a new variable and tried to change the text of my textView. the error is cannot implicitly convert type 'System.Threading.Tasks.Task<string>' to 'string'
  • Nkosi
    Nkosi almost 7 years
    The method is returning a Task<string> You would have to get it from the task's .Result property, but that will cause a deadlock because of the mixing of async/await and blocking calls.
  • Frank Mascia
    Frank Mascia almost 7 years
    I see, sorry I'm brand new to this, so is there a different way of going about this that wont cause a deadlock?
  • Frank Mascia
    Frank Mascia almost 7 years
    When I call your synchronous version of the method I get System.NullReferenceException: Object reference not set to an instance of an object. and its lighting up the var response = client.GetAsync(request).Result; line
  • Nkosi
    Nkosi almost 7 years
    That is bevause the client is null. you never initialized it.
  • Frank Mascia
    Frank Mascia almost 7 years
    added client = new HttpClient()' to the top of the synchronous method, that should initialize it right? Now when its run I get a System.AggregateException: One or more errors occurred still lighting up that same line
  • Nkosi
    Nkosi almost 7 years
    Well you need to debug ans see what those error are. Most probably you have a bad url or something else