Answer the question
In order to leave comments, you need to log in
An embedded statement cannot be a declaration or a statement with an identifier?
private void getWmiDecodeFromNhtsaApi()
{
string url = "https://vpic.nhtsa.dot.gov/api/vehicles/DecodeWMI/" + wmi + "?format=json";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
try
{
var tmp = client.GetAsync(url).Result;
if (tmp.IsSuccessStatusCode)
var result = tmp.Content.ReadAsStringAsync();
}
catch (Exception err)
{
// error handling
}
}
There is a request for api, code: var result = tmp.Content.ReadAsStringAsync();
Answer the question
In order to leave comments, you need to log in
The official documentation also contains a lot of bad code, even from microsoft.
Define your method in async and get rid of bad code with ".Result;"
Without delving into the essence of your code, specifically this error is as follows:
You have an operator embedded in an if that is a variable declaration (and its initialization). Swears, because the operation is meaningless. After exiting the if block, the result will be lost. Therefore, we first declare the variable, and then we initialize it inside the if:
Task<string> result;
if (tmp.IsSuccessStatusCode)
result = tmp.Content.ReadAsStringAsync();
if (tmp.IsSuccessStatusCode) {
var result = tmp.Content.ReadAsStringAsync();
Console.WriteLine(result.Result);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question