HttpComponents HttpClient: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 8: | Line 8: | ||
* [[Apache Foundation#Subjects|Apache Foundation]] | * [[Apache Foundation#Subjects|Apache Foundation]] | ||
= | =Multithreaded Execution= | ||
{{External|http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d5e405}} | |||
In order to allow execution of multiple HTTP requests concurrently on multiple threads, httpclient must be equipped with a pooling connection manager such as <tt>PoolingClientConnectionManager</tt>. | |||
=Redirect Handling= | |||
{{External|http://hc.apache.org/httpcomponents-client-4.5.x/tutorial/html/fundamentals.html#d5e334}} | |||
Turn it off: | |||
<pre> | |||
RedirectStrategy redirectStrategy = new RedirectStrategy() { | |||
@Override | |||
public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { | |||
return false; | |||
} | |||
@Override | |||
public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { | |||
throw new RuntimeException("getRedirect() NOT YET IMPLEMENTED"); | |||
} | |||
}; | |||
... HttpClients.custom().setRedirectStrategy(redirectStrategy).build(); | |||
</pre> | |||
=Troubleshooting= | |||
==httpclient Automatically Follows Redirects== | |||
Sometimes this is not the behavior you want, for example when you try to get the OAuth token which is embedded in the redirect Location that you are supposed to parse. See [[#Redirect_Handling|Redirect Handling]] above. |
Revision as of 17:06, 20 March 2017
External
- http://hc.apache.org/httpcomponents-client-ga/
- tutorial http://hc.apache.org/httpcomponents-client-ga/tutorial/html/index.html
Internal
Multithreaded Execution
In order to allow execution of multiple HTTP requests concurrently on multiple threads, httpclient must be equipped with a pooling connection manager such as PoolingClientConnectionManager.
Redirect Handling
Turn it off:
RedirectStrategy redirectStrategy = new RedirectStrategy() { @Override public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { return false; } @Override public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { throw new RuntimeException("getRedirect() NOT YET IMPLEMENTED"); } }; ... HttpClients.custom().setRedirectStrategy(redirectStrategy).build();
Troubleshooting
httpclient Automatically Follows Redirects
Sometimes this is not the behavior you want, for example when you try to get the OAuth token which is embedded in the redirect Location that you are supposed to parse. See Redirect Handling above.