apache-httpclient-4.x

fix java.net.SocketTimeoutException: Read timed out

我只是一个虾纸丫 提交于 2019-11-28 22:56:39
问题 I have a RESTful server which takes an http POST input from client to vote up songs on server. I have used Apache HTTPClient for client. public boolean vote() { HttpClient client = new DefaultHttpClient(getHttpParameters()); HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout Limit HttpResponse response; try { HttpPost post = new HttpPost("http://127.0.0.1:8080/ws/"); StringEntity se = new StringEntity("{ \"song_id\" : \"2\" }"); se.setContentType(new BasicHeader

Java HttpRequest JSON & Response Handling

大城市里の小女人 提交于 2019-11-28 22:54:19
问题 I have looked at several other questions, but I still don't fully understand this. I want to POST a JSON string to a remote address and then retrieve the values from the JSON response. I am using the Apache libraries for Java. public HttpResponse http(String url, String body) throws IOException { try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { HttpPost request = new HttpPost(url); StringEntity params = new StringEntity(body); request.addHeader("content-type",

How to get HttpClient returning status code and response body?

怎甘沉沦 提交于 2019-11-28 22:22:24
I am trying to get Apache HttpClient to fire an HTTP request, and then display the HTTP response code (200, 404, 500, etc.) as well as the HTTP response body (text string). It is important to note that I am using v4.2.2 because most HttpClient examples out there are from v.3.x.x and the API changed greatly from version 3 to version 4. Unfortunately I've only been able to get HttpClient returning the status code or the response body (but not both). Here's what I have: // Getting the status code. HttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet("http://whatever.blah.com

Apache Camel Http and SSL

被刻印的时光 ゝ 提交于 2019-11-28 21:40:41
问题 I have been trying to get a 2-way ssl/https proxy working with Camel. I have been able to set up the Jetty Component using 2-way ssl and am now attempting to get it working with the Http4 component to complete the client side of the proxy. When I route the jetty traffic to a log component, all is well and the 2 way ssl trust chain is fine. When I throw in the Http4 component, it blows up with a peer not authenticated exception. I am using Camel 2.7.0 Here is what I have so far public static

Android: Scheme 'http' not registered on ICS 4.0.4 w/ proxy

守給你的承諾、 提交于 2019-11-28 21:34:00
I'm using HttpClient for HTTPS requests, which has worked fine up until now. After upgrading to ICS, some users are reporting problems connecting on 3G connections. EDIT: Most of them seem to be using a proxy, and I can reproduce this locally with a T-Mobile SIM using their proxy. The logs have this stack trace: java.lang.IllegalStateException: Scheme 'http' not registered. org.apache.http.conn.scheme.SchemeRegistry.getScheme(SchemeRegistry.java:80) org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:126) org.apache.http.impl.conn

Exception : javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated

江枫思渺然 提交于 2019-11-28 19:31:36
问题 public HttpClientVM() { BasicHttpParams params = new BasicHttpParams(); ConnManagerParams.setMaxTotalConnections(params, 10); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setUseExpectContinue(params, false); HttpConnectionParams.setStaleCheckingEnabled(params, true); HttpConnectionParams.setConnectionTimeout(params, 30000); HostnameVerifier hostnameVerifier= org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER; HttpsURLConnection

How to - Spring IoC and HttpClient 4.3.1 CloseableHttpClient?

一曲冷凌霜 提交于 2019-11-28 18:05:51
问题 I'd like to have Spring IoC configure a CloseableHttpClient object and inject it into my class so that customization of its configuration can be done via XML. From what I can see, HttpClient seems to resist this pattern quite forcibly. They want you to do things like CloseableHttpClient chc = HttpClients.custom().set<thing that should be a property>().build(); Ick. Is there not some mechanism for making a singleton CloseableHttpClient bean that I can then use? 回答1: This seems to work for me:

Why did the author use EntityUtils.consume(httpEntity);?

前提是你 提交于 2019-11-28 17:49:59
I've come across EntityUtils.consume(httpEntity); and I'm not sure what it really does. For example: try { //... some code HttpEntity httpEntity = httpResponse.getEntity(); BufferedReader br = new BufferedReader(new InputStreamReader(http.Entity.getContent())); String line; while ((line = br.readLine())!= null) { System.out.println(line); } EntityUtils.consume(httpEntity); } catch (Exception e) { //code } finally { httpClient.getConnectionManager().shutdown(); } Why did the author put in EntityUtils.consume(httpEntity); when the finally block will close the connection and garbage collector

How to set custom User-Agent with apache http client library 4.1?

一曲冷凌霜 提交于 2019-11-28 17:26:22
How to make HTTPClient use custom User-Agent header? The following code submits empty user-agent. What am I missing? import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.CoreProtocolPNames; import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils;

Apache HttpComponents HttpClient timeout

試著忘記壹切 提交于 2019-11-28 15:47:13
问题 How do I set the connection timeout in httpcomponents httpclient? I have found the documentation at: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html but it is not clear how these parameters are actually set. Also, an explanation of the difference between SO_TIMEOUT and CONNECTION_TIMEOUT would be helpful. 回答1: In version 4.3 of Apache Http Client the configuration was refactored (again). The new way looks like this: RequestConfig.Builder requestBuilder =