apache-httpclient-4.x

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

血红的双手。 提交于 2019-11-27 10:48:57
问题 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

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

你。 提交于 2019-11-27 10:28:44
问题 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

Android: IllegalStateException in HttpGet

北战南征 提交于 2019-11-27 09:51:14
I'm trying to send a GET request using HttpClient , but I keep getting an IllegalStateException . Any idea what's causing this? I've been looking around for a solution, but I don't get what it means by "host=null" in the log. How do I set the host, and how is it different from the path? Here's my logcat out: 07-17 11:54:18.002: W/System.err(15422): java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=google.com 07-17 11:54:18.002: W/System.err(15422): at org.apache.http.impl.client.DefaultRequestDirector.determineRoute

What is the difference between the setConnectionTimeout , setSoTimeout and “http.connection-manager.timeout” in apache HttpClient API

只愿长相守 提交于 2019-11-27 09:17:33
问题 What is the difference between the three(marked as comments) : MultiThreadedHttpConnectionManager connManag = new MultiThreadedHttpConnectionManager(); HttpConnectionManagerParams managParams = connManag.getParams(); managParams.setConnectionTimeout(connectiontimeout); // 1 managParams.setSoTimeout(sotimeout); //2 HttpMethodBase baseMethod = null; try { HttpClient client = new HttpClient(connManag); client.getParams().setParameter("http.connection-manager.timeout", poolTimeout); //3

Apache HTTPClient DigestAuth doesn't forward “opaque” value from Challenge

六眼飞鱼酱① 提交于 2019-11-27 08:42:18
问题 I'm trying to use Digest authentication with HTTP Client against a 3rd-party web service that I don't control. I started out with the sample code from here: http://hc.apache.org/httpcomponents-client-4.5.x/httpclient/examples/org/apache/http/examples/client/ClientPreemptiveDigestAuthentication.java I got it working against httpbin.org , before attempting the next step described below. It appears that the target 3rd-party service that I'm using requires the opaque value to be copied from the

HttpClientBuilder basic auth

匆匆过客 提交于 2019-11-27 08:22:16
Since HttpClient 4.3, I have been using the HttpClientBuilder. I am connecting to a REST service that has basic authentication. I am setting the credentials as follows: HttpClientBuilder builder = HttpClientBuilder.create(); // Get the client credentials String username = Config.get(Constants.CONFIG_USERNAME); String password = Config.get(Constants.CONFIG_PASSWORD); // If username and password was found, inject the credentials if (username != null && password != null) { CredentialsProvider provider = new BasicCredentialsProvider(); // Create the authentication scope AuthScope scope = new

Does Apache Commons HttpClient support GZIP?

吃可爱长大的小学妹 提交于 2019-11-27 08:15:11
Does the library Apache Commons HttpClient support Gzip? We wanted to use enable gzip compression on our Apache server to speed up the client/server communications (we have a php page that allows our Android application to sync files with the Server). Apache HttpClient 4.1 supports content compression out of the box along with many other features that were previously considered out of scope. If your server is able to provide GZIPped content, with Apache Http client 4.1 all you need is to use org.apache.http.impl.client.ContentEncodingHttpClient which is a subclass of DefaultHttpClient . This

javax.net.ssl.SSLPeerUnverifiedException: Host name does not match the certificate subject provided by the peer

柔情痞子 提交于 2019-11-27 07:27:38
I follow many links on stackoverflow and tried many solutions, but none of them worked for me. I'm using WSO2 API manager version 1.9.1 . I am facing following error: Exception in thread "main" javax.net.ssl.SSLPeerUnverifiedException: Host name 'XXXXXXXXX' does not match the certificate subject provided by the peer (CN=localhost, O=WSO2, L=Mountain View, ST=CA, C=US) at org.apache.http.conn.ssl.SSLConnectionSocketFactory.verifyHostname(SSLConnectionSocketFactory.java:465) at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:395) at org

Apache HttpClient 4.1 - Proxy Authentication

社会主义新天地 提交于 2019-11-27 07:27:34
I've been trying to configure the user and password for proxy authentication from the configured properties while using Apaches HttpComponent's httpclient, but with no success. All examples I have found refer to methods and classes that are no longer available, such as HttpState and setProxyCredentials . So, can anyone give me an example of how to configure the proxy credentials? For Basic-Auth it looks like this: DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getCredentialsProvider().setCredentials( new AuthScope("PROXY HOST", 8080), new UsernamePasswordCredentials(

How to switch from HttpClient to HttpUrlConnection?

ⅰ亾dé卋堺 提交于 2019-11-27 07:08:33
问题 I am creating an Android application and I send data from Android application to servlet through HttpClient. I use HttpPost method. I read in Android developer site that Apache HttpClient library has some bug in Android Froyo 2.2 and after all it's good practice to use HttpUrlConnection instead HttpPost. So I want to convert my HttpPost code to HttpUrlConnectio but don't know how. I am posting my Android code as well as servlet code here Android code private String postData(String