Board index » delphi » Delphi2007, Indy10, TidHttp, site returns 500 but has content

Delphi2007, Indy10, TidHttp, site returns 500 but has content


2007-10-13 03:21:55 AM
delphi280
Hi,
Sample code.
##
try
FHTTP.Get(AURI, Result);
except
on E: Exception do
begin
if (Result.Size = 0) and (Result.Position = 0) then
Result.CopyFrom(FHTTP.Response.ContentStream, 0)
end;
Problem:
##
If website returns reponse code 500, an exception
is thrown, and there is no way to get content.
This is a general problem with some variants of IIS
/ ASP pages server (at least only encountered
it with websites that appear to use above).
IE, FireFox etc. can view website even with response
code 500 (verified using FireFox Live HTTP Headers
which also states website returns response code 500).
I will be happy to email link to people if they do not
believe above scenario can happen, but I prefer not
to post it in usenet as it is not my website :-)
Anyways, does anyone have a workaround how
to get the content? Preferably still by using Indy.
best regards
Thomas Schulz
 
 

Re:Delphi2007, Indy10, TidHttp, site returns 500 but has content

"dk_sz" <XXXX@XXXXX.COM>writes
Quote
Sample code.
The "Result" and "Response.ContentStream" are pointing to the same TStream
object. The TStream you pass to Get() gets assigned to the
Response.ContentStream property. So you are copying an empty TStream back
into itself.
To access the real error data, you need to catch an EIdHTTPProtocolException
exception and then read its ErrorMessage property. For example:
try
FHTTP.Get(AURI, Result);
except
on E: EIdHTTPProtocolException do
begin
// use E.ErrorCode, E.Message, and E.ErrorMessage properties as
needed...
end;
on E: Exception do
begin
// a non-HTTP error occured, do something else...
end;
end;
Quote
If website returns reponse code 500, an exception
is thrown, and there is no way to get content.
Yes, there is. You are simply looking in the wrong place for it.
Gambit
 

Re:Delphi2007, Indy10, TidHttp, site returns 500 but has content

Quote
Response.ContentStream property. So you are copying an empty TStream back
into itself.
Indeed, just trying to figure out where I can
get the body content response that is sent!
(Even though the server returns response code 500)
Quote
To access the real error data, you need to catch an
EIdHTTPProtocolException
exception and then read its ErrorMessage property. For example:
Yes, there is. You are simply looking in the wrong place for it.
on E: EIdHTTPProtocolException do
begin
// use E.ErrorCode, E.Message, and E.ErrorMessage properties as
OK, thanks for the info! I did not think that the body
content stream of the website would be returned in
the exception object, but I will try look! Thanks!
best regards
Thomas Schulz
 

Re:Delphi2007, Indy10, TidHttp, site returns 500 but has content

Quote
Indeed, just trying to figure out where I can
get the body content response that is sent!
(Even though the server returns response code 500)


>you need to catch an EIdHTTPProtocolException
>exception and then read its ErrorMessage property.
I can't find any stream object holding the webpage
content. I wish to get the webpage content as a stream.
At least it does not appear in the exception object
as far as I can see Remy? Perhaps someone can
enlighten me where to get my eyesight checked? *G*
There is no way to retrieve that in Indy if the
webpage gives response code 500 I think.
best regards
Thomas Schulz
 

Re:Delphi2007, Indy10, TidHttp, site returns 500 but has content

Quote
Sample code.
FHTTP.Get(AURI, Result);
The Result stream holds the text but you have to
reset the Result.Position := 0;
in order to read it from the stream again.
 

Re:Delphi2007, Indy10, TidHttp, site returns 500 but has content

Quote
>If website returns reponse code 500, an exception
>is thrown, and there is no way to get content.
The Result stream holds the text but you have to
reset the Result.Position := 0;
in order to read it from the stream again.
If it was just that easy :)
The Result stream does not hold the text.
The *Size* property of the Result stream is 0
The *Position* property of the Result stream is 0
I can tell you what I think. I think that when TidHTTP
receives server response code 500 (which is "internal
server error") in response headers, it does not expect
to receive any actual content and therefore drops trying.
Infact, a quick search on usenet shows other
people have suffered from the same problem,
but no solution have appeared.
best regards
Thomas Schulz
 

Re:Delphi2007, Indy10, TidHttp, site returns 500 but has content

"dk_sz" <XXXX@XXXXX.COM>writes
Quote
I can't find any stream object holding the webpage
content.
The EIdHTTPProtocolException.ErrorMessage property holds the page data as a
String, not a Stream.
Quote
I wish to get the webpage content as a stream.
The data is not given to you as a stream. You will have to copy it from the
ErrorMessage property into your own stream.
Quote
There is no way to retrieve that in Indy if the
webpage gives response code 500 I think.
Yes, there is. I already told you were to get it from.
Gambit
 

Re:Delphi2007, Indy10, TidHttp, site returns 500 but has content

"Dennis Passmore" <XXXX@XXXXX.COM>writes
Quote
The Result stream holds the text
It won't if an HTTP error occurs. TIdHTTP will temporarily replace the
user's stream with its own stream that is created internally, then read the
error data into that temp stream and construct the EIdHTTPProtocolException
content from it, then finally replace the temp stream with the user's
original stream. So the user's stream gets contents only when no HTTP error
occurs.
Gambit
 

Re:Delphi2007, Indy10, TidHttp, site returns 500 but has content

"dk_sz" <XXXX@XXXXX.COM>writes
Quote
I can tell you what I think.
What you think is not what actually happens. I have already explained to you
where to get the error data.
Quote
I think that when TidHTTP receives server response code
500 (which is "internal server error") in response headers,
it does not expect to receive any actual content and therefore
drops trying.
Yes, it does expect data, and it does not drop it. As I told you earlier,
you are simply looking in the wrong place for it.
Quote
Infact, a quick search on usenet shows other
people have suffered from the same problem,
but no solution have appeared.
I already gave you the definitive solution.
Gambit
 

Re:Delphi2007, Indy10, TidHttp, site returns 500 but has content

Quote
>I can't find any stream object holding the webpage
>content.
The EIdHTTPProtocolException.ErrorMessage property holds the page data as
a String, not a Stream.
It seems you are correct. I am not sure I
understand why ErrorMessage is used for
that, but I guess you have had your reasons.
So the page data is still raw I suppose?
(Just stored in AnsiString instead of Stream)
So I just write the whole content into stream...
And it is business as usual from that point on.
(On the stream I check HTTP headers, meta tag
HTTP equalants, BOM etc. myself to detect e.g.
UTF-8 and UTF-16 so I can convert properly)
Thanks for the help
best regards
Thomas Schulz
 

Re:Delphi2007, Indy10, TidHttp, site returns 500 but has content

"dk_sz" <XXXX@XXXXX.COM>writes
Quote
It seems you are correct. I am not sure I
understand why ErrorMessage is used for
that, but I guess you have had your reasons.
Why should it be anything else?
Quote
So the page data is still raw I suppose?
(Just stored in AnsiString instead of Stream)
Yes.
Gambit
 

Re:Delphi2007, Indy10, TidHttp, site returns 500 but has content

Quote
>So the page data is still raw I suppose?
>(Just stored in AnsiString instead of Stream)

Yes.
Thanks for the help again :-)
Quote
>It seems you are correct. I am not sure I
>understand why ErrorMessage is used for
>that, but I guess you have had your reasons.

Why should it be anything else?
Personal preference I guess. I think it would be more
intuitive if everything went to the user supplied stream.
But probably too late to change this now anyways.
best regards
Thomas Schulz
 

Re:Delphi2007, Indy10, TidHttp, site returns 500 but has content

"dk_sz" <XXXX@XXXXX.COM>writes
Quote
Personal preference I guess. I think it would be more
intuitive if everything went to the user supplied stream.
That wouldn't be a good idea when an error occurs. The stream would not
contain the type of data the user was expecting to receive. That could be
disasterous depending on the type of stream being used. Imagine what could
happen if a database blob or flat file failed parsing later on because
erroneous data was saved to it.
Gambit
 

Re:Delphi2007, Indy10, TidHttp, site returns 500 but has content

Quote
That wouldn't be a good idea when an error occurs. The stream would not
contain the type of data the user was expecting to receive. That could be
disasterous depending on the type of stream being used. Imagine what
could
happen if a database blob or flat file failed parsing later on because
erroneous data was saved to it.
On those cases where above holds true, it would
be quite easy to check for response code 200 etc?
(Or simply check the data downloaded.)
Anyways, I realize one can't go back now.
best regards
Thomas Schulz