HttpClient - setting a "global" socket timeout, and a separate timeout per request
Tag : java , By : Keonne Rodriguez
Date : March 29 2020, 07:55 AM
Hope this helps With HttpClient, I am setting the default socket/connection timeout with the following: , If you are using HttpClient 4.0 you could do this : mClient = new DefaultHttpClient(connectionManager, params) {
protected HttpParams determineParams(HttpRequest req) {
//Fill in your impl here
}
|
HttpClient request succeeds with timeout defined, but hangs without
Tag : java , By : Wilfred Knigge
Date : March 29 2020, 07:55 AM
may help you . I found the answer! Turns out HttpClient only allows a certain number of connections at a time. According to my code, the default maximum connections is 2. I needed to close each connection after they were complete and the upload ran fine. Fixed code adds request connection release. private void uploadParts(String assetId) throws IOException {
//set up post request
HttpClient client = HttpClientBuilder.create().build();
String url = "";
//prepare video
File video = new File("files/video.mp4");
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(video));
int partMaxSize = 1024 * 1024 * 5;
byte[] buffer = new byte[partMaxSize];
double fileSize = video.length();
System.out.println(fileSize);
System.out.println(fileSize / partMaxSize);
int parts = (int) Math.ceil(fileSize / partMaxSize);
System.out.println(parts);
for(int i = 1; i < parts+1; i++) {
String partNumber = i + "";
System.out.println("part: " + partNumber);
int partSize = (int) (i < parts ? partMaxSize : fileSize);
fileSize -= partSize;
int tmp = 0;
tmp = bis.read(buffer);
url = String.format("https://www.site.com/upload/multipart/%s/%s", assetId, partNumber);
final HttpPut request = new HttpPut(url);
request.addHeader("Authorization", "Bearer " + accessToken);
request.addHeader("Content-Type", "application/octet-stream");
request.setEntity(new ByteArrayEntity(buffer));
//Magical code start
int hardTimeout = 5; // seconds
TimerTask task = new TimerTask() {
@Override
public void run() {
if (request != null) {
request.abort();
}
}
};
new Timer(true).schedule(task, hardTimeout * 1000);
//Magical code end
HttpResponse response = client.execute(request);
request.releaseConnection();
System.out.println(response.getStatusLine().getReasonPhrase());
}
bis.close();
}
|
HTTPClient POST Hangs/Timeout Exception
Tag : chash , By : PatrickSimonHenk
Date : March 29 2020, 07:55 AM
like below fixes the issue For some reason the process could not be multithreaded. Switched from Tasks to blocking single threaded it worked fine...
|
Re-using HttpClient but with a different Timeout setting per request?
Tag : .net , By : Yolanda N. Ceron
Date : March 29 2020, 07:55 AM
seems to work fine Under the hood, HttpClient just uses a cancellation token to implement the timeout behavior. You can do the same directly if you want to vary it per request: var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromSeconds(30));
await httpClient.GetAsync("http://www.google.com", cts.Token);
httpClient.Timeout = System.Threading.Timeout.InfiniteTimeSpan;
|
HttpClient getting timed out even after setting Timeout.Infinite
Tag : .net , By : omaidog
Date : March 29 2020, 07:55 AM
this will help I have tried following code for Android and iOS and it seems working and can wait little longer on socket while fetching data using webservice in Xamarin.Android following code is used and exposed it using DependencyService public HttpClientHandler GetHttpClientHandler()
{
Xamarin.Android.Net.AndroidClientHandler http = new Xamarin.Android.Net.AndroidClientHandler();
http.ReadTimeout = TimeSpan.FromMinutes(5.0);
return http;
}
HttpMessageHandler IHttpHandler.GetHttpHandler()
{
NSUrlSessionConfiguration sessionConfig = NSUrlSessionConfiguration.DefaultSessionConfiguration;
sessionConfig.TimeoutIntervalForRequest = 300;
sessionConfig.TimeoutIntervalForResource = 300;
sessionConfig.WaitsForConnectivity = true;
NSUrlSessionHandler sessionHandler = new NSUrlSessionHandler(sessionConfig);
return (sessionHandler);
}
HttpClient client = new HttpClient(DependencyService.Get<IHttpClientHandler>().GetHttpClient());
|