I am trying to use the status to see if there is an error in C++ XPCOM component in my observer class on Mac.
OnStateChange(
nsIWebProgress *aWebProgress,
nsIRequest *aRequest,
PRUint32 aStateFlags,
nsresult aStatus)
{
}
In proxy environment the aStatus
parameter is always true though the browser fails to load the page.
In non-proxy environments it gives the proper value (error) in status.
You can see it if you try accessing http://10.1.3.3/ (some random IP). With a proxy th开发者_如何学Goe status is zero (success) and without a proxy you get an error value.
Should some parameters be set to get the proper error value?
This is the expected behavior if you use an HTTP proxy. A non-zero aStatus
means "didn't get any response because of some error". On the other hand, a zero aStatus
value means "there was some response, check nsIHttpChannel.responseStatus
to see whether the request was successful". That's what you get if the server responds with "404 Not Found" for example - aStatus
will be zero (you got a response back) but nsIHttpChannel.responseStatus
will be 404.
It's the same with an HTTP proxy because the proxy will always send a response back, probably "502 Bad Gateway" if it couldn't connect to the server. That's what the browser gets so aStatus
will be zero and nsIHttpChannel.responseStatus
will be 502. So in you code you should do something like this:
OnStateChange(
nsIWebProgress *aWebProgress,
nsIRequest *aRequest,
PRUint32 aStateFlags,
nsresult aStatus)
{
if (FAILED(aStatus))
{
// Got no response
}
else
{
nsCOMPtr<nsIHttpChannel> channel = do_QueryInterface(aRequest);
PRUint32 status = 0;
if (channel)
channel->GetResponseStatus(&status);
if (status >= 400)
{
// Got an HTTP error
}
else
{
// Success!
}
}
}
精彩评论