Cannot convert from 'string' to 'System.Net.Http.HttpRequestMessage'
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));
Frank Mascia
Updated on December 03, 2022Comments
-
Frank Mascia 29 days
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
calledtestText
. I'm open to using eitherRestSharp
or theHttpClient
, 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 theCannot 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 over 5 yearsplease 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 over 5 yearsDone, sorry was keeping those in incase someone thought those were a better route to go.
-
-
Frank Mascia over 5 yearsBoth 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 iscannot implicitly convert type 'System.Threading.Tasks.Task<string>' to 'string'
-
Nkosi over 5 yearsThe 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 over 5 yearsI 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 over 5 yearsWhen 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 thevar response = client.GetAsync(request).Result;
line -
Nkosi over 5 yearsThat is bevause the
client
is null. you never initialized it. -
Frank Mascia over 5 yearsadded
client = new HttpClient()'
to the top of the synchronous method, that should initialize it right? Now when its run I get aSystem.AggregateException: One or more errors occurred
still lighting up that same line -
Nkosi over 5 yearsWell you need to debug ans see what those error are. Most probably you have a bad url or something else