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 ripok <rk...@gmail.com> on 2008/02/29 15:36:34 UTC

Whole HTTP response as InputStream?

I have a little problemo: I'm making HTTP request to a server that gives me
back a kind of mime message in the response, so the content-type of the HTTP
response will be "Content-Type: multipart/related;boundary='xxxx'". 

When I'm parsing that mime message (I use mime4j), the parser needs that
content type declaration because there is the boundary definition and it is
part of the message. However if I just get the response body from
HttpMethod, that content-type declaration is not within it because it's in
the HTTP response headers.
 
One way I can handle this is to get the response body as string (or byte
array) and add that content-type response header to the first line of the
body before giving it to mime parser. However I don't like this because I
want to handle the response as an InputStream to prevent storing the whole
response in memory before handling (the response can be quite big).

So is there a way to get the whole HTTP response as an InputStream, so that
there are the response headers with it. Or can I somehow insert that content
type header to to beginning of response body InputStream?
I'm using HttpClient version 3.1.

-ripok-
-- 
View this message in context: http://www.nabble.com/Whole-HTTP-response-as-InputStream--tp15759846p15759846.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: Whole HTTP response as InputStream?

Posted by Alan Moss <am...@learningsoft.net>.
Use a java.io.SequenceInputStream:

   //construct contentTypeHeader here

   StringBuffer buf = new StringBuffer(contentTypeHeader);
   SequenceInputStream seqIn =
       new SequenceInputStream(
           new StringBufferInputStream(buf),
           method.getResponseBodyAsStream());

   //do something with seqIn here.

HTH,
-Alan


Sam Berlin wrote:
> Could you create a custom InputStream that took the header & response
> inputstream in its constructor, and returned one and then the other
> when reading?
> 
> Sam


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Whole HTTP response as InputStream?

Posted by Sam Berlin <sb...@gmail.com>.
Could you create a custom InputStream that took the header & response
inputstream in its constructor, and returned one and then the other
when reading?

Sam

On 2/29/08, ripok <rk...@gmail.com> wrote:
>
> I have a little problemo: I'm making HTTP request to a server that gives me
> back a kind of mime message in the response, so the content-type of the HTTP
> response will be "Content-Type: multipart/related;boundary='xxxx'".
>
> When I'm parsing that mime message (I use mime4j), the parser needs that
> content type declaration because there is the boundary definition and it is
> part of the message. However if I just get the response body from
> HttpMethod, that content-type declaration is not within it because it's in
> the HTTP response headers.
>
> One way I can handle this is to get the response body as string (or byte
> array) and add that content-type response header to the first line of the
> body before giving it to mime parser. However I don't like this because I
> want to handle the response as an InputStream to prevent storing the whole
> response in memory before handling (the response can be quite big).
>
> So is there a way to get the whole HTTP response as an InputStream, so that
> there are the response headers with it. Or can I somehow insert that content
> type header to to beginning of response body InputStream?
> I'm using HttpClient version 3.1.
>
> -ripok-
> --
> View this message in context: http://www.nabble.com/Whole-HTTP-response-as-InputStream--tp15759846p15759846.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: Whole HTTP response as InputStream?

Posted by ripok <rk...@gmail.com>.
Now I understand what you are meaning. 

The situation here is that the server is mixing mime message headers in http
metadata (response headers). You are right that the whole mime message
_should_ be in the payload (=http response body), but that server is managed
by third party vendor so we don't have control to change that.

-ripok-


Tatu Saloranta wrote:
> 
> 
> So if there are headers that are logically part of the
> mime message, they belong to payload (from httpclient
> perspective); but if you were talking about http
> headers, they'd be part of response metadata, not
> payload.
> 
> 

-- 
View this message in context: http://www.nabble.com/Whole-HTTP-response-as-InputStream--tp15759846p15845836.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: Whole HTTP response as InputStream?

Posted by Tatu Saloranta <co...@yahoo.com>.
--- ripok <rk...@gmail.com> wrote:

...
> Tatu, I don't understand what you mean at the end of
> your response? The
> whole HTTP response, which server sends back, is the
> mime message. So the
> response headers are also part of returned mime
> message.

I guess it depends on types of headers we are talking
about (and I may have misunderstood you). But
basically there's difference between physical part
(yes, all the data comes from same stream, via same
socket) and logical part (layering -- http
server/client usually should handle http-level headers
that preced payload).

So if there are headers that are logically part of the
mime message, they belong to payload (from httpclient
perspective); but if you were talking about http
headers, they'd be part of response metadata, not
payload.

Does this make sense? Anyway, if you can easily solve
the problem using SequenceInputStream, go for it. :-)

-+ Tatu +-



      ____________________________________________________________________________________
Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ 


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Whole HTTP response as InputStream?

Posted by ripok <rk...@gmail.com>.
Sorry the late response...

Yes, I think that SequenceInputStream is my answer. Thank you very much Alan
and Tatu for the tip.

Tatu, I don't understand what you mean at the end of your response? The
whole HTTP response, which server sends back, is the mime message. So the
response headers are also part of returned mime message.

-ripok-


Tatu Saloranta wrote:
> 
> Can you perhaps just use java.io.SequenceInputStream,
> by creating simple dummy input stream for content type
> part (new
> ByteArrayInputStream(headerString.getBytes([whatever-encoding
> -you-got])) and chain this with body contents?
> 
> It also seems strange that a library (mime4j) would
> require access to parts it basically shouldn't need,
> so perhaps there is a way to pass this information
> some other way than it being part of stream?
> 
> Hope this helps,
> 
> -+ Tatu +-
> 

-- 
View this message in context: http://www.nabble.com/Whole-HTTP-response-as-InputStream--tp15759846p15822055.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: Whole HTTP response as InputStream?

Posted by Tatu Saloranta <co...@yahoo.com>.
Can you perhaps just use java.io.SequenceInputStream,
by creating simple dummy input stream for content type
part (new
ByteArrayInputStream(headerString.getBytes([whatever-encoding
-you-got])) and chain this with body contents?

It also seems strange that a library (mime4j) would
require access to parts it basically shouldn't need,
so perhaps there is a way to pass this information
some other way than it being part of stream?

Hope this helps,

-+ Tatu +-

--- ripok <rk...@gmail.com> wrote:

> 
> I have a little problemo: I'm making HTTP request to
> a server that gives me
> back a kind of mime message in the response, so the
> content-type of the HTTP
> response will be "Content-Type:
> multipart/related;boundary='xxxx'". 
> 
> When I'm parsing that mime message (I use mime4j),
> the parser needs that
> content type declaration because there is the
> boundary definition and it is
> part of the message. However if I just get the
> response body from
> HttpMethod, that content-type declaration is not
> within it because it's in
> the HTTP response headers.
>  
> One way I can handle this is to get the response
> body as string (or byte
> array) and add that content-type response header to
> the first line of the
> body before giving it to mime parser. However I
> don't like this because I
> want to handle the response as an InputStream to
> prevent storing the whole
> response in memory before handling (the response can
> be quite big).
> 
> So is there a way to get the whole HTTP response as
> an InputStream, so that
> there are the response headers with it. Or can I
> somehow insert that content
> type header to to beginning of response body
> InputStream?
> I'm using HttpClient version 3.1.
> 
> -ripok-
> -- 
> View this message in context:
>
http://www.nabble.com/Whole-HTTP-response-as-InputStream--tp15759846p15759846.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
> 
> 



      ____________________________________________________________________________________
Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  http://tools.search.yahoo.com/newsearch/category.php?category=shopping

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org