Why I can not get Timeout on HttpWebRequest? (HttpWebRequest Timeout nightmare) - httpwebrequest

I'm using it in Quartz.Net framework. Every Job(thread) has only one web request
Here is the check list:
All disposable objects are used in using block!
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
str = reader.ReadToEnd();
ServicePointManager.Expect100Continue = false;
ServicePointManager.DefaultConnectionLimit = 1000;
ServicePointManager.MaxServicePoints = 1000;
request.KeepAlive=false
request.Timeout = 5000;
request.ReadWriteTimeout = 5000;
request.ServicePoint.ConnectionLeaseTimeout = 5000;
request.ServicePoint.MaxIdleTime = 5000;
I'm using Stopwatch to measure web request time. These are that logs:
Abnormal Web Request time: 16333 ms
Abnormal Web Request time: 8350 ms
Abnormal Web Request time: 9846 ms
Abnormal Web Request time: 7545 ms
Abnormal Web Request time: 6662 ms
Abnormal Web Request time: 18332 ms
Any Idea?
PS: This question is summarized version of Timeout supported Multithreaded Web Request

Only lesson that I learned from this problem: If you are using HttpWebRequest in multiple threads don't use it synchronous!

Related

Restrict size of response from client side

Is there a way to restrict the size of the http response content while forming the http request? We use the following piece of code to execute a GET request and intend to handle responses of size <= 1MB
HttpUriRequest httpUriRequest = new HttpGet(this.getResolvedEndpoint());
HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
HttpResponse response = httpClient.execute(httpUriRequest, context);
HttpEntity entity = response.getEntity();
axios provides support for a parameter maxContentLength which helps in capping the size of the http response size. We are looking for an equivalent solution in JAVA.

Net Core HttpClient is disconnected after 2 minutes

I have a .NET 5 webapi application. Another .NET 5 console application is sending requests to the webapi. I have 10 seconds timeout on each request and I am sending requests one by one with a short interval. On my PC (which is very fast) I send thousands of requests and all of them succeed. But on another PC (which is pretty slow) I get few request failed, approximately every 2 minutes. First request takes a few seconds to send when the application starts because of connection estabishment to webapi.
var httpClient = new HttpClient
{
Timeout = TimeSpan.FromSeconds(10)
};
await PerformPingAsync(httpClient, interval, cts.Token).ConfigureAwait(false);
private static async Task PerformPingAsync(HttpClient httpClient, int interval, CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
try
{
var request = new HttpRequestMessage
{
RequestUri = new Uri("http://127.0.0.1/api/service/ping"),
Method = HttpMethod.Get
};
var response = await httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
_consoleLogger.Info("Ping attempt succeed");
}
catch (Exception e)
{
_consoleLogger.Error(e);
}
await Task.Delay(TimeSpan.FromSeconds(interval), cancellationToken).ConfigureAwait(false);
}
}
Interval between requests is 1 second.
I have tried .net core 3.1, .NET 5 and 6 for my console ping application - and all of them have request timeouts (10 seconds). I have created the same console application in .net framework 4.8 and I get no timeouts. I also created a html page with JS fetch to send request and I get no timeouts.
Also what I have discovered that these timeouts can start to happen after a PC reboot. After some of reboots I might not get any timeouts but after the other reboot - I have them.
I found out that HttpClient acts a bit different in .net core/.NET 5/6 than in .net framework. And I tried to use SocketsHttpHandler:
var socketsHandler = new SocketsHttpHandler
{
PooledConnectionLifetime = TimeSpan.FromMinutes(10),
PooledConnectionIdleTimeout = TimeSpan.FromMinutes(10)
};
return new HttpClient(socketsHandler)
{
Timeout = TimeSpan.FromSeconds(10)
};
But I have no success. Due to the official documention PooledConnectionLifetime timer should be reset after request is sent. If I have no requests for 10 minutes the connection will be closed. It's obvious that my N request is timed out because connection is being reestablished. But why it's happening, why my SocketsHttpHandler is not working? Any ideas?

Onelogin Logging a User In Via API not working

I'm using the onelogin REST api to log a user in: https://developers.onelogin.com/api-docs/1/samples/login-user-via-api.
I have followed all the steps successfully to generate a session token with no issues.
The documentation then says to post the session token to this url: https://admin.us.onelogin.com/session_via_api_token
However, when do the post to that URL with the session token it simply re-directs me to the onelogin Sign On Page.
Here is the c# code for the post. I have a valid session token in variable: session_token:
string url = "https://admin.us.onelogin.com/session_via_api_token";
StringBuilder postData = new StringBuilder();
postData.Append("session_token=" + HttpUtility.UrlEncode(session_token) + "&");
postData.Append("auth_token=" + HttpUtility.UrlEncode(""));
//ETC for all Form Elements
// Now to Send Data.
StreamWriter writer = null;
request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.ToString().Length;
try
{
writer = new StreamWriter(request.GetRequestStream());
writer.Write(postData.ToString());
}
finally
{
if (writer != null)
writer.Close();
}
This appears to be server side code so this will never be able to successfully get a session with the end-user's browser.
In order for this flow to work properly, you need to redirect the end-user's browser to the https://admin.us.onelogin.com/session_via_api_token URL with just the auth_token value as a POST parameter.
All the above code will do is allow your back end server to get a session cookie, which doesn't help your end-user establish a session at all.
More details can be found here: https://developers.onelogin.com/api-docs/1/samples/login-user-via-api

Unable to consume OpenShift REST API in C#.net

I want to know how can I consume OpenShift Rest API into C#.net based application. I have gone through URL
https://access.redhat.com/documentation/en-US/OpenShift_Online/2.0/pdf/REST_API_Guide/OpenShift_Online-2.0-REST_API_Guide-en-US.pdf, in this there mentioned example for Ruby, Python and cRUL. but not mentioned for .Net. so I have created sample application for consuming this api.
below is the code -
string URL = "https://openshift.redhat.com/broker/rest/api";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "GET";
request.ContentType = "application/xml;";
try
{
WebResponse webResponse = request.GetResponse();
Stream webStream = webResponse.GetResponseStream();
StreamReader responseReader = new StreamReader(webStream);
string response = responseReader.ReadToEnd();
Console.Out.WriteLine(response);
responseReader.Close();
}
catch (Exception e)
{
Console.Out.WriteLine("-----------------");
Console.Out.WriteLine(e.Message);
}
While executing above code I am getting following error -
"The remote server returned an error: (406) Not Acceptable."
Please let me where I am doing wrong.
Thanks in advance
Jyoti
You are using the wrong HTTP header. ContentType is used for POST/PUT operations to tell server what to expect. When you GET a resource you must specify an Accept header.
request.Accept = "application/xml";

Unable to connect web exception mvc4

we are doing a facebook posting application using HttpWebRequest.
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
//doing some stuff here.
}
This code is failing at line uisng with unable to connect to server exception.
This is not failing always, but it is failing in 90% cases.
Can somebody advise how best this can be addressed?
Try this (this may not be an answer; just trying to debug your issue), see if it fails like your other one does; then try replacing google.com with your own URL:
WebRequest g = HttpWebRequest.Create("http://www.google.com");
var response = g.GetResponse();