• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 710
  • Last Modified:

How do I get http status code from IE browser control?

Hi,

I could not find any method or property which returns the HTTP status code in Internet Explorer control (IE 5.5).  Currently I am guessing the status code by checking the title of the page.

Any help will be greatly appreciated.

Thanks.
0
sgovinda
Asked:
sgovinda
1 Solution
 
ShaunWildeCommented:
what do you mean the 404 type response? the contents of the page is where it will be - the browser hides all inner workings and trnslates the status messages into a web page if it is an error.

If you need the raw status codes you will have to use the WinInet functions yourself - but that means you will have trouble rendering it
0
 
cypherljkCommented:
I'm not real familiar with that control but is it possible to call the GetReadyState() function? Just a thought I couldn't find much documentation.

My 2 cents...
0
 
DanRollinsCommented:
Specifically, what status are you trying to get?  And if you get it, what would you want to do with it?

In you OnDocumentComplete handler, you can get access to the IHTMLDocument2 interface and perhaps learn something by scraping the contents.  If nothing else, you can get the URL to see if it what you think it should be.

I don't see any clear way to get the actual HTTP status (eg, the 404 or 401 code).  One possibility would be to provide an OnBeforeNavigate2 handler.  Then use CHttpConnection/CHttpFile to pre-read the page and check for an error.  If all is OK, then pass it on to the normal handler.  But there could easily be a better way.

-- Dan
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
MRNMurthyCommented:
Hi,

If u use the CHttpFile class then u will be able to get the status code from it is method QueryInfoStatusCode( DWORD& dwStatusCode ) to which u have to pass referrence of a DWORD. For the details u can go thru the MSDN even sample code is available in it.

From

MRN Murthy

0
 
sgovindaAuthor Commented:
Thanks for the response guys.

I am working on an application which automates the browser and extracts the content of a page.  To decide whether I have a valid page, http status code is a must.

Events like BeforeNavigate2, DocumentComplete etc is raised even for a failure condition.  Parsing the title or content of a page to find the http status code is unreliable.

I cannot use CHttpFile because in case of secure pages (for example SSL), I have to handle all the authentication in my code.  I am sure it is easier to let the browser do the job.

I still wonder why IE is hiding such a simple and important data like http status code. (considering the rich API it provides for parsing the page)


0
 
DanRollinsCommented:
>>I cannot use CHttpFile because in case of secure pages
>>(for example SSL), I have to handle all the athentication
>>in my code.  I am sure it is easier to let the browser do the job.

Given this, then I think you already know what is happeneing... the Webrbrowser control is doing exactly what you want it to do!  It is handling the 40x errors for you!

By the time you get control at OnDocumentComplete, there *is* no HTTP error code for you to process (it would always be 200, "no error")

So now to solve your problem....

You are writing code to automate walking HTML pages, so there is no reason to use a Webrowser control at all -- unless you need a UI for display-only functionality.  SO just drop-dwon to using CHTTPConnection and CHttpFile... but that leaves the problem of handling security such as HTTPS connections.

That turns out to be pretty easy....

After CHttpFile::SendRequest(), you can call pFile->QueryInfoStatusCode or QueryInfo to obtain error info.  When you get back HTTP_STATUS_DENIED, then you can programmatically handle it.  See:

http://msdn.microsoft.com/workshop/networking/wininet/overview/authentication.asp

Also, the "TEAR" sample on your MSDN disk shows most of what you need to do to grab pages without a web browser.  This link to MS seems to be broken, but it might start working later:

80% TEAR: "Tearing" HTML Pages Off the Internet  
http:// msdn.microsoft.com / library / en-us / vcsample98 / html / _sample_mfc_tear.asp  

http://msdn.microsoft.com/library/en-us/vcsample98/html/_sample_mfc_tear.asp

-==-=-=-=-=-

A reasonable compromise would be to use both methods -- a WebBrowser control in which you pre-access each URL in the OnBeforeNavigate2 handler.  You have rejected this suggestion previously, so I won't bother to show you any sample code, but if you change your mind, let me know.

-- Dan
0
 
DanRollinsCommented:
hi sgovinda,
Do you have any additional questions?  Do any comments need clarification?

-- Dan
0
 
sgovindaAuthor Commented:
Hi Dan,

The solution you were suggesting doesn't fit my needs exactly. Also. it is quite an amount of work (my schedule doesn't permit it right now).  I would rather try your points at my own leisure.

I have committed to give points anyway.  I can as well give it you.  If you have few samples, you can post it, it would be helpful.

Thanks.


0
 
DanRollinsCommented:
Sorry sgovinda,
I have found it unprofitable to workout example code and otherwise spend time solving problems for persons who admit up front that they don't like the obvious solution.

-- Dan
0
 
sgovindaAuthor Commented:
Hi Dan,

I am giving you the points anyway.  If you don't have some samples ready at hand, I don't expect you to spend time on the problem.

Thanks to all the guys who responded to this question.
sgovinda.  
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now