apache-httpcomponents

How to set RequestBody for Http Delete method.

爷,独闯天下 提交于 2019-12-10 14:43:39
问题 I am writing a client code for a server which Delete API. The API specification requires data to be sent. I am using HttpComponents v3.1 library for writing client code. Using the HtpDelete class I could not find a way to add request data to it. Is there a way to do so ? Below is the code snippet. HttpDelete deleteReq = new HttpDelete(uriBuilder.toString()); List<NameValuePair> postParams = new ArrayList<NameValuePair>(); postParams.add(new BasicNameValuePair(RestConstants.POST_DATA_PARAM

Do I need to close expired connections in Apache HTTPAsyncClient?

跟風遠走 提交于 2019-12-10 13:56:24
问题 Do I need to close expired connections in Apache HttpAsyncClient as is done with HttpClient? 回答1: I just double-checked the source code. Yes, one still has to explicitly evict expired connections from the pool. However , the underlying NIO channel and socket will get closed and released immediately. The problem with expired connections not being automatically evicted from the pool is a bug in HttpAsyncClient 4.0 beta3. Feel free to raise a JIRA for this defect. 来源: https://stackoverflow.com

How to post array parameters with HttpComponents

假如想象 提交于 2019-12-08 04:32:20
问题 I want to perform this command with Apache http-components (4.1.2) curl --data "strings[]=testOne&string-keys[]=test.one&strings[]=testTwo&string-keys[]=test.two&project=Test" https://api.foo.com/1/string/input-bulk The target api need strings and string-keys parameters as array , which mean repeating strings[] and string-keys[] for each parameter. This curl command works fine but with Http-component, while I got exactly the same parameters. Maybe I'm doing something wrong. List<NameValuePair

MultipartEntity: Content-Length for InputStream

ⅰ亾dé卋堺 提交于 2019-12-07 15:00:15
问题 I'm trying to send the data from an inputstream in a multipart/form-data, as a file-parameter using: MultipartEntityBuilder.create() .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) .addBinaryBody("file", inputStream) .build(); the problem is that the server seems to require a Content-Length header. I know the correct size of my inputStream - can I set it manually? 回答1: Instead of using the addBinaryBody method, you can create your own FormBodyPart with a ContentBody . The appropriate

In httpclient what is the most elegant/correct way to turn HttpEntity to a String?

南笙酒味 提交于 2019-12-07 04:58:47
问题 I'm fetching a web page using the Apache httpcomponents Java library. After connecting the result I get is an HttpEntity which has a method getContent() which returns an InputStream and also has a method writeTo() which writes to an OutputStream. I want to turn the result into a String for extracting information. What is the most elegant (and safe) way to do this? Some possible solutions: Write to a ByteArrayOutputStream and then convert those bytes to a String with a String constructor use

Spring configurable, high performance, metered http client instances

谁都会走 提交于 2019-12-06 05:48:29
问题 Coming from DropWizard I am used to its HttpClientConfiguration and I am baffled that in Spring Boot I cannot find some support for controlling in a similar manner http clients instances to be used, by RestTemplates for example. To work in production the underlying client implementation should be high performance (e.g. non blocking io, with connection reuse and pooling). Then I need to set timeouts or authentication, possibly metrics gathering, cookie settings, SSL certificates settings. All

Importing a newer Apache HttpClient jar in Android

安稳与你 提交于 2019-12-06 00:20:56
问题 I'm trying to send an HTTP/HTTPS post request from my Android client. Question Why does my code fail? So far I created an apache class/HttpClient call. Everything worked fine: HttpClient httpClient = new DefaultHttpClient(); I've read that this method has been deprecated, so I've switched to the new recommended method: HttpClient httpClient = HttpClientBuilder.create().build(); Eclipse didn't have this class, so I had to download Apache HttpClient 4.3.3. I imported it to my project by copying

MultipartEntity: Content-Length for InputStream

风格不统一 提交于 2019-12-05 20:47:52
I'm trying to send the data from an inputstream in a multipart/form-data, as a file-parameter using: MultipartEntityBuilder.create() .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) .addBinaryBody("file", inputStream) .build(); the problem is that the server seems to require a Content-Length header. I know the correct size of my inputStream - can I set it manually? Instead of using the addBinaryBody method, you can create your own FormBodyPart with a ContentBody . The appropriate ContentBody is InputStreamBody but its getContentLength method returns -1 . I'd suggest you extend the class to

In httpclient what is the most elegant/correct way to turn HttpEntity to a String?

依然范特西╮ 提交于 2019-12-05 12:56:50
I'm fetching a web page using the Apache httpcomponents Java library . After connecting the result I get is an HttpEntity which has a method getContent() which returns an InputStream and also has a method writeTo() which writes to an OutputStream. I want to turn the result into a String for extracting information. What is the most elegant (and safe) way to do this? Some possible solutions: Write to a ByteArrayOutputStream and then convert those bytes to a String with a String constructor use InputStreamReader to read straight from the stream, and put into a StringBuilder Both of these feel a

Default timeout for HttpComponent Client

こ雲淡風輕ζ 提交于 2019-12-04 16:48:40
问题 I can't find any documentation on the default httpParams for httpclient 4.1 ? What's the default socket timeout when I do a GET ? 回答1: According to the documentation, the http.socket.timeout parameter controls the SO_TIMEOUT value, and: If this parameter is not set, read operations will not time out (infinite timeout). 回答2: The accepted answer is not applicable for newer versions of HttpClient. Versions 4.3.X and above use the system default which is usually 60 secs. Taken from HttpClient