I found this peculiar behaviour of UWP HttpClient.
For a simple get call of WCF service is taking time almost more than 100 seconds(happens for first few calls). I observed that the same service is way too faster in iOS and rest client.
Attaching the code, Please let me know, if i doing wrong.
HttpClientHandler clientHandler = new HttpClientHandler();
clientHandler.UseCookies = true;
var client = new HttpClient(clientHandler);
client.Timeout = TimeSpan.FromSeconds(100);
client.DefaultRequestHeaders.Add("Accept", "application/json");
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get,
requestUri);
var response = await client.SendAsync(httpRequestMessage);
//Between these lines there is a delay, it doesnt fire time out
also. I tried with HttpCompletionOption. No luck
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
}
Any help will be really appreciated.
Thanks and Regards,
Rummy
Related
I am a beginner to GET and POST. I apologize if the question sounds stupid. But I need your help in guiding me to do it the correct way.
I have to periodically do GET from server A indefinitely until user terminate using Ctrl + C
To check at interval I use the timer below
Timer timer = new Timer(RunTask, null, 3000, 3000);
Where RunTask is the callback method consists of GET method to retrieve data below
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(urlA);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject<RootObject>(responseBody);
}
Then register the data to server B through POST method
using (HttpClient client = new HttpClient())
{
var json = JsonConvert.SerializeObject(rootIssue);
var output = new StringContent(json, Encoding.UTF8, "application/json");
var result = await client.PostAsync(urlB, output);
string response = result.Content.ReadAsStringAsync().Result;
}
Once registered, server B will return with updated info which I have to POST back to server A
using (HttpClient client = new HttpClient())
{
var json = JsonConvert.SerializeObject(rootIssue);
var output = new StringContent(json, Encoding.UTF8, "application/json");
var result = await client.PostAsync(urlA, output);
string response = result.Content.ReadAsStringAsync().Result;
}
My question here is, what is the best approach for me to do all these queries in my console app? Should I separate each GET and POST to these
GetFromA()
PostToB()
PostToA()
I am concerned if I do it this way, is it possible that PostToB() will POST with incomplete response from GetFromA()?
Or should I combine GET and POST from A to B in one method to something like
GetFromAPostToB()
I hope that someone can point me in the correct direction.
Thanks.
I have a problem about 502 gateway error in my production server.
I try to develop an application which calls azure rest endpoints. I get response with Postman by sending the endpoint which has following documentation:
https://learn.microsoft.com/en-us/rest/api/azure/devops/wit/classification-nodes/get?view=azure-devops-rest-6.0
While Postman or Invoke-RestMethod work well with azure endpoint, but API, developed by me, based on HttpClient/RestSharp give an 502 Bad gateway error. HttpClient code part can be found below:
var personalaccesstoken = "<my-token>";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "<my-email-address>", personalaccesstoken))));
HttpRequestMessage requestMessage = new HttpRequestMessage();
requestMessage.Method = HttpMethod.Get;
requestMessage.RequestUri = new Uri("<my-azure-server-address>/_apis/wit/classificationNodes/Areas?$depth=100");
using (HttpResponseMessage response = client.SendAsync(requestMessage).Result)
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
return responseBody;
}
}
How can I solve the problem or which tool can I use to find the exact problem?
Thank you in advance:
I've been trying for a day to make this work synchronously, not async.
Here is the code that works:
HttpClientHandler handler = new HttpClientHandler();
handler.UseDefaultCredentials = false;
handler.Credentials = new NetworkCredential(username, password);
HttpClient clientPageFirst = new HttpClient(handler);
HttpResponseMessage responsePageFirst = await clientPageFirst.GetAsync(fullURL);
HttpContent contentPageFirst = responsePageFirst.Content;
string resultPageFirst = await contentPageFirst.ReadAsStringAsync();
Console.WriteLine(resultPageFirst);
It's for a C# console app, and there is another call there to another platform's API which works synchronously, but it uses tokens in the header to validate, not a network credential like this one (calling a local on premise CRM URL).
Can someone please help me change the
HttpResponseMessage responsePageFirst = await clientPageFirst.GetAsync(fullURL);
line so it is synchronous?
T.I.A.
Try this
HttpResponseMessage responsePageFirst = clientPageFirst.GetAsync("fullURL").Result;
There is this webservice, which i need to get some information. This webservice has an Authentication Header, and two parameters embedded in the body of the request. I have wrote the method using HttpClient class:
HttpClient httpClient = new HttpClient();
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpRequestMessage request = new HttpRequestMessage();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
request.RequestUri = new Uri("URL");
request.Method = HttpMethod.Post;
request.Headers.Add("AUTHENTICATION_Key", "AUTHENTICATION_VALUE");
request.Content = new StringContent("{\"P1\":\"V1\",\"P2\":\"V2\"}",Encoding.UTF8,"application/json");
HttpResponseMessage response = await httpClient.SendAsync(request);
if (response.StatusCode == HttpStatusCode.OK)
{
var responseString = await response.Content.ReadAsStringAsync();
}
the problem has risen from where the project I need to use, is using DotNet 2.0 and in .Net 2 we cannot use asynchronize therefore I cannot use await and SendAsync() as well as ReadAsStringAsync() another problem is SecurityProtocolType.Tls12 is not available in .Net 2. and Im going to need it to get data from it.
I appriciate the help.
I'm very new on this and I need some help. I'm trying to send a notification from my webapi to my app. To do this a need just send a post to the url("https://onesignal.com/api/v1/notifications") with some informations (Header from authorization and content-type). But when I send the post it takes a long and and I just get The operation timeout has been reached, no message errors that could help me. I tryed the code from onesignal's documentation from asp.net solutions but isn't worked for me. Anyone can help? Or just help how can I trace the error with my requisiton? After try the code from onesignal's documentation I decided use the following code(both codes had the same behavior):
using (var client = new HttpClient())
{
var url = new Uri("https://onesignal.com/api/v1/notifications");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "Rest ID");
var obj = new
{
app_id = "APP ID",
contents = new { en = "English Message" },
included_segments = new string[] { "All" }
};
var json = JsonConvert.SerializeObject(obj);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync(url, content);
}
For some reason is taking a long time to send a notification and return some response for me. So I increase the timeout to wait the response and don't get a task cancelled. Hope help someone.