You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by nabbler101 <bg...@gmail.com> on 2010/01/20 17:54:30 UTC
UrlEncodedFormEntity throws OutOfMemoryError exception
So I'm trying to pass a very large string (~15mb) as a parameter using
httppost but I'm getting an OutOfMemoryException. The heap size is set to
512-1024mb (which should be more than enough.)
Is there a way around this problem? I'd appreciate any help.
Here's the code:
----------------------------------------------------------------------------------
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xxxx/xxx");
// read file into string
byte[] buffer = new byte[(int) new File(filePathName).length()];
BufferedInputStream f = new BufferedInputStream(new
FileInputStream(filePathName));
f.read(buffer);
String str = new String(buffer);
// create name value pairs for post
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("A", "en"));
nvps.add(new BasicNameValuePair("B", "1"));
nvps.add(new BasicNameValuePair("C", str));
httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
// throws the exception
System.out.println("executing request " + httppost.getURI());
// Create a response handler
ResponseHandler<String> responseHandler = new
BasicResponseHandler();
String responseBody = httpclient.execute(httppost, responseHandler);
System.out.println(responseBody);
httpclient.getConnectionManager().shutdown();
------------------------------------------------------------------------------
--
View this message in context: http://old.nabble.com/UrlEncodedFormEntity-throws-OutOfMemoryError-exception-tp27244948p27244948.html
Sent from the HttpClient-User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org
Re: UrlEncodedFormEntity throws OutOfMemoryError exception
Posted by David Motes <da...@gmail.com>.
Use FileBody in the MultipartEntity.
See MultipartEntity discussion in this post:
http://old.nabble.com/HttpClient-3.1-to-4.0-migration-td25262394.html
On Thu, Jan 21, 2010 at 8:14 AM, nabbler101 <bg...@gmail.com> wrote:
>
> Yes I've tried MultipartEntity. That works fine. But I want to send two
> text parameters a 3rd parameter as a file.
>
> Lets say A and B are the two text parameters
>
> nvps.add(new BasicNameValuePair("A", "1"));
> nvps.add(new BasicNameValuePair("B", "2"));
>
> But C is a file...a text file at that.
>
> nvps.add(new BasicNameValuePair("C", "big text file"));
>
> Now I know that's not a good idea, but what is the correct way of doing
> this?
>
> Thanks!
>
>
>
> olegk wrote:
>>
>> On Wed, 2010-01-20 at 08:54 -0800, nabbler101 wrote:
>>> So I'm trying to pass a very large string (~15mb) as a parameter using
>>> httppost but I'm getting an OutOfMemoryException. The heap size is set
>>> to
>>> 512-1024mb (which should be more than enough.)
>>>
>>> Is there a way around this problem? I'd appreciate any help.
>>>
>>
>> What is the point of buffering 15mb of data in memory? Have you
>> considered NOT buffering content of the file in memory?
>>
>> Oleg
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
>> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>>
>>
>>
>
> --
> View this message in context: http://old.nabble.com/UrlEncodedFormEntity-throws-OutOfMemoryError-exception-tp27244948p27257481.html
> Sent from the HttpClient-User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org
Re: UrlEncodedFormEntity throws OutOfMemoryError exception
Posted by nabbler101 <bg...@gmail.com>.
Yes I've tried MultipartEntity. That works fine. But I want to send two
text parameters a 3rd parameter as a file.
Lets say A and B are the two text parameters
nvps.add(new BasicNameValuePair("A", "1"));
nvps.add(new BasicNameValuePair("B", "2"));
But C is a file...a text file at that.
nvps.add(new BasicNameValuePair("C", "big text file"));
Now I know that's not a good idea, but what is the correct way of doing
this?
Thanks!
olegk wrote:
>
> On Wed, 2010-01-20 at 08:54 -0800, nabbler101 wrote:
>> So I'm trying to pass a very large string (~15mb) as a parameter using
>> httppost but I'm getting an OutOfMemoryException. The heap size is set
>> to
>> 512-1024mb (which should be more than enough.)
>>
>> Is there a way around this problem? I'd appreciate any help.
>>
>
> What is the point of buffering 15mb of data in memory? Have you
> considered NOT buffering content of the file in memory?
>
> Oleg
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>
>
--
View this message in context: http://old.nabble.com/UrlEncodedFormEntity-throws-OutOfMemoryError-exception-tp27244948p27257481.html
Sent from the HttpClient-User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org
Re: UrlEncodedFormEntity throws OutOfMemoryError exception
Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2010-01-20 at 08:54 -0800, nabbler101 wrote:
> So I'm trying to pass a very large string (~15mb) as a parameter using
> httppost but I'm getting an OutOfMemoryException. The heap size is set to
> 512-1024mb (which should be more than enough.)
>
> Is there a way around this problem? I'd appreciate any help.
>
What is the point of buffering 15mb of data in memory? Have you
considered NOT buffering content of the file in memory?
Oleg
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org