I am trying to call a web api and I want to see what response I get. WOuld it be 200, 204 or 500.
I am trying it fot thr first time.
public void foo()
{
RunAsync(); // dont know what it return type will be
}
static async Task RunAsync()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:9000/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync("pid=23&lang=en-us");
}
}
Here, the code stops on the last line. How can I do that ?
You can use the StatusCode property of your HttpResponseMessage.
HttpResponseMessage response = await client.GetAsync("pid=23&lang=en-us");
if (response.IsSuccessStatusCode)
{
//was success
var result = await response.Content.ReadAsStringAsync();
//checck result string now
//you can also deserialize the response to your custom type if needed.
}
else
{
var statusCode = response.StatusCode;
//do something with this
}
Here is the official documentation of HttpStatusCode enumeration which gives you a complete list of possible status code values.
Since your method returns a Task, you should await it when you call it.
public async Task foo()
{
await RunAsync();
}
Related
My code to fetch a huge set of data from an API is like this
public static async Task<model> GetDataAsyncs(string url)
{
// Initialization.
mymodel responseObj = new mymodel();
using (var httpClientHandler = new HttpClientHandler())
{
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
using (var client = new HttpClient(httpClientHandler))
{
// Setting Base address.
client.BaseAddress = new Uri(apiBasicUri);
// Setting content type.
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// Initialization.
HttpResponseMessage response = new HttpResponseMessage();
// HTTP Get
response = await client.GetAsync(url ).ConfigureAwait(false);
// Verification
if (response.IsSuccessStatusCode)
{
// Reading Response.
string result = response.Content.ReadAsStringAsync().Result;
responseObj.Status = true;
responseObj.Data = result;
}
}
}
return responseObj;
}
And I am calling above function like this inside my controller
public ActionResult myActionMethod()
{
string res= helper.GetDataAsync("url").Result.Data;
}
Ocassionally this throws an error system.threading.tasks.taskcanceledexception a task was canceled . This does not occurs every time. Can anyone please point out what I am doing wrong here?
I can't say for sure why this is happening, but there are some red flags in your code that can be cleaned up and might resolve this.
The first is your use of .ConfigureAwait(false). It can cause some unintended consequences, so I suggest you don't use it. I talk about it more in an article I recently wrote.
Second, use await instead of .Result whenever possible, which is almost always. Using .Result can also cause unintended, hard-to-debug consequences. In your code, I see no reason you can't use await.
Third, the documentation of HttpClient says:
HttpClient is intended to be instantiated once and re-used throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. This will result in SocketException errors.
So you can declare a static HttpClient and reuse that every time you need it.
Fourth, there's no need for this line:
HttpResponseMessage response = new HttpResponseMessage();
You're instantiating a new HttpResponseMessage here, but then immediately overwriting it in the next line.
Making those changes, your code could look like this:
private static HttpClient _client = new HttpClient(
new HttpClientHandler {ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; }}
) {
BaseAddress = new Uri(apiBasicUri),
DefaultRequestHeaders = {
Accept = { new MediaTypeWithQualityHeaderValue("application/json") }
}
};
public static async Task<model> GetDataAsyncs(string url)
{
mymodel responseObj = new mymodel();
var response = await _client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsStringAsync();
responseObj.Status = true;
responseObj.Data = result;
}
return responseObj;
}
And then change your controller action to be async and use await:
public async Task<ActionResult> myActionMethod()
{
var res = (await helper.GetDataAsync("url")).Data;
}
See if you still end up getting exceptions after making those changes.
Working on a project where a WPF front end, and trying to get a handle on async calls to HttpClient and I've been going around and around trying to get PostAsync to work, but it routinely appears to deadlock, or at least the post response times out, even with large values for timeout, and a visible response in fiddler.
So, after a while I decided to try out a few other methods on HttpClient, and they worked, first try. No idea why.
I'm clean all the way to my WPF button with awaits, asyncs, and .ConfigureAwait(false) (I think):
Button:
private async void Generate_Suite_BTN_Click(object sender, RoutedEventArgs e)
{
await suiteBuilder.SendStarWs().ConfigureAwait(false);
}
XmlDoc Load:
internal async Task SendStarWs()
{
var xmlDoc = new XmlDocument();
xmlDoc.Load("C:\\Temp\\file.xml");
await StarWSClient.SendStarMessage(xmlDoc).ConfigureAwait(false);
}
SendMessage:
private static readonly HttpClient Client = new HttpClient {MaxResponseContentBufferSize = 1000000};
public static async Task<STARResult> SendMessage(vars)
{
var response = await SendRequestAsync(url, contentNew, Client).ConfigureAwait(false);
return new STARResult(response, hash);
}
This call '500s' against my endpoint immediately, which I'd expect:
var response = await SendRequestAsync(url, contentNew, Client).ConfigureAwait(false);
private static async Task<HttpResponseMessage> SendRequestAsync(string adaptiveUri, StringContent content, HttpClient httpClient)
{
HttpResponseMessage responseMessage = null;
try
{
responseMessage = await httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Post, adaptiveUri)).ConfigureAwait(false);
}
catch (Exception ex)
{
if (responseMessage == null)
responseMessage = new HttpResponseMessage
{
StatusCode = HttpStatusCode.InternalServerError,
ReasonPhrase = $"SendRequestAsync failed: {ex.Message}"
};
}
return responseMessage;
}
The Post variant returns a TaskCancellationException, with timeout message regardless of timeout value:
var response = await PostRequestAsync(url, contentNew, Client).ConfigureAwait(false);
private static async Task<HttpResponseMessage> PostRequestAsync(string adaptiveUri, StringContent content, HttpClient httpClient)
{
HttpResponseMessage responseMessage = null;
try
{
responseMessage = await httpClient.PostAsync(adaptiveUri, content).ConfigureAwait(false);
}
catch (Exception ex)
{
if (responseMessage == null)
responseMessage = new HttpResponseMessage
{
StatusCode = HttpStatusCode.InternalServerError,
ReasonPhrase = $"PostRequestAsync failed: {ex.Message}"
};
}
return responseMessage;
}
My endpoint responds normally to our other software, so I'm pretty sure the endpoint is solid, I can't fathom why the post response is blocked, when the send isn't.
SendAsync can make any http verb request depending on how you set that property. PostAsync and similar are just convenience methods. Those convenience methods use SendAsync internally which is why when you derive a handler you only need to override SendAsync and not all of the send methods.
To your other question though:
When you use SendAsync you need to create the content and pass it. Your only sending an empty message. The 500 likely means that the api got null from the model binding and kicked you back. Just as #John commented.
I am making a POST request to a route which is returning JSON data.
[HttpPost("api/v1/testGetAll")]
public object Test([FromBody]object filteringOptions)
{
return myService.GetLogs(filteringOptions).ToArray();
}
Route works fine, filtering works fine, and when I test the route in Postman I get the right response. However this is only a back-end, and I would like to invoke this route from my custom API gateway.
The issue I'm facing is getting that exact response back. Instead I am getting success status, headers, version, request message etc.
public object TestGetAll(string ApiRoute, T json)
{
Task<HttpResponseMessage> response;
var url = ApiHome + ApiRoute;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
try
{
response = client.PostAsync(url, new StringContent(json.ToString(), Encoding.UTF8, "application/json"));
return response.Result;
}
catch (Exception e)
{
...
}
}
}
How can I get exact content back?
You need to read the content from response.
var contentString = response.Result.Content.ReadAsStringAsync().Result;
If you wish, you can then deserialize the string response into the object you want returning.
public async Task<TResult> TestGetAll<TResult>(string apiRoute, string json)
{
// For simplicity I've left out the using, but assume it in your code.
var response = await client.PostAsJsonAsync(url, json);
var resultString = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<TResult>(resultString);
return result;
}
You have to return the response as an HttpResponseMessage.
Try changing your return statement to
[HttpPost("api/v1/testGetAll")]
public IHttpActionResult Test([FromBody]object filteringOptions)
{
return Ok(myService.GetLogs(filteringOptions).ToArray());
}
Please note: This will return the response with status code 200. In case you want to handle the response based on different response code. You can create the HttpResponseMessage like this-
Request.CreateResponse<T>(HttpStatusCode.OK, someObject); //success, code- 200
Request.CreateResponse<T>(HttpStatusCode.NotFound, someObject); //error, code- 404
T is your object type.
And so on...
I'm not sure, but it appears to me that the default implementation of .NET HttpClient library is flawed. It looks like it sets the Content-Type request value to "text/html" on a PostAsJsonAsync call. I've tried to reset the request value, but not sure if I'm doing this correctly. Any suggestions.
public async Task<string> SendPost(Model model)
{
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await client.PostAsJsonAsync(Url + "api/foo/", model);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
You should set the content type. With the Accept you define what you want as response.
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
The Accept request-header field can be used to specify certain media types which are acceptable for the response. Accept headers can be used to indicate that the request is specifically limited to a small set of desired types, as in the case of a request for an in-line image.
public async Task<string> SendPost(Model model)
{
var client = new HttpClient(); //You should extract this and reuse the same instance multiple times.
var request = new HttpRequestMessage(HttpMethod.Post, Url + "api/foo");
using(var content = new StringContent(Serialize(model), Encoding.UTF8, "application/json"))
{
request.Content = content;
var response = await client.SendAsync(request).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
}
I previously posted a question about using HTTPClient with async/await. Now I'm trying to figure out how to do this in such a way as to actually make the Post calls execute at the same time while still being able to handle the resulting HttpResponseMessage.
This is what I've come up with. However, being a noob to await/async, I'm still unsure about if I'm doing this correctly or not. Could someone verify that this is the proper way to do this ... or at least a proper way.
public async Task ProcessAsync() {
//Query lists
List<Member> list = dbContext.Users.Where(u => u.IsMember).ToList();
//Add members to mailing list through web service
await AddMembersAsync(list);
}
private async Task AddMembersAsync(List<Member> members) {
using(var _client = new HttpClient()) {
//Initialize Http Client
...
var responses = await Task.WhenAll(members.Select(x => PostMemberAsync(x,_client)));
await Task.WhenAll(responses.Select(r => ProcessResponseAsync(r,client)));
}
}
private async Task<HttpResponseMessage> PostMemberAsync(Member member, HttpClient client) {
var jss = new JavaScriptSerializer();
var content = jss.Serialize(new MemberPost() {
email_address = member.email,
...
});
return await client.PostAsync("uri",new StringContent(content, Encoding.UTF8, "application/json"));
}
private async Task ProcessResponsesAsync(HttpResponseMessage response, HttpClient client) {
if(response.IsSuccessStatusCode) {
var responseText = await response.Content.ReadAsStringAsync();
var jss = new JavaScriptSerializer();
var userid = jss.Deserialize<MemberResponse>(responseText);
//Store mailing user's id
...
}
response.Dispose();
}
This appears to me like it would be correct. However, I have a slight problem with this. I need to tie each HttpResponseMessage to the member for which the message was created. My current code only returns Task but the response message does not contain a link to the user. (The service I'm posting to returns an id specific to the service. I need to keep track of that Id for each user so that I have a link between the member id and the service id).
Does my requirement of linking the id from the response message to the member make it unrealistic to use the above code or is there a way to somehow return the member as part of the task results?
I'm suggesting this without trying it out so please be careful but I would replace these two lines:
var responses = await Task.WhenAll(members.Select(x => PostMemberAsync(x,_client)));
await Task.WhenAll(responses.Select(r => ProcessResponseAsync(r,client)));
with this:
await Task.WhenAll(members.Select(async x =>
{
var response = await PostMemberAsync(x, _client);
await ProcessResponseAsync(response, client, x);
}));
And of course you need to enhance ProcessResponseAsync by the argument Member
I need to tie each HttpResponseMessage to the member for which the message was created.
When doing asynchronous programming, I find it useful to avoid side effects. In other words, if you have a method that calculates or determines something, return that value from the method rather than saving it in some member variable.
private async Task<MemberResponse> ProcessResponseAsync(HttpResponseMessage response, HttpClient client)
{
using (response)
{
if(response.IsSuccessStatusCode)
{
var responseText = await response.Content.ReadAsStringAsync();
var jss = new JavaScriptSerializer();
var userid = jss.Deserialize<MemberResponse>(responseText);
return userid;
}
else
{ ... }
}
}
Add a small helper method, and your calling code becomes quite clean:
private async Task<HttpResponseMessage> ProcessMemberAsync(Member member, HttpClient client)
{
var response = await PostMemberAsync(member, client);
return await ProcessResponseAsync(response, client);
}
private async Task AddMembersAsync(List<Member> members)
{
using(var client = new HttpClient())
{
... // Initialize HttpClient
var responses = await Task.WhenAll(members.Select(x => ProcessMemberAsync(x, client)));
for (int i = 0; i != members.Count; ++i)
{
var member = members[i];
var response = responses[i];
...
}
}
}