Error handling WEBBrowser

HARRY

Active Member
Licensed User
Longtime User
Hi,

I am using the WEBBrowser library to download and display Bing images (maps).
In general it works find, but from time to time only an empty rectangle with a small red cross in it is displayed.

In fact I am using the REST services of Bing maps. The documentation says that in case of errors error codes are given.

The question now is : "How to find the error code?".

May be that the HTTP library gives a solution, but I do not know how to use it in combination with the WEBBrowser.

Can somebody help me?

Harry
 

Basic4Life

Member
Licensed User
Probably simplest, yet not most elegant solution, is to do a "HEAD" request with the HTTP lib, check that ResponseCode and then procede with the WebBrowser. You can find an example here
I'm not familiar with the API but I noticed while trying out some example urls in some cases it responds with a xml or json document containing the error code.
So depending on which kind of queries you are using it might be possible to only use the webbrowser.
On the other hand if you only get image files, it might be possible to do all the downloading with the HTTP lib and use the webbrowser only to display the data.
Thus reducing the amount of requests you have to make, especially important if there are restricted developer keys for using the API, but as I said I'm not familiar with the API.
If you like you can post your source code and we can have a closer look on how to implement it the best way.
 

HARRY

Active Member
Licensed User
Longtime User
Hi Basic4Life,

Thanks for your response. I tried the ReadyState_2 example. With a valid URL it gives always the error: Error message ;file not found. With valid I mean, working well in my own program.

If I comment the line:requestCheck.Method = "HEAD" it works fine. I get the impression that now the map is loaded twice, which I can not afford.

I do not know how to download a map just using the HTTP library and then displaying the map in the browser. Why browser anyway, why not in an image object? This combined with an error code if downloading fails.

By the way; if downloading fails, it may succeed after 1 or 5 or 10 seconds with exactly the same URL. Probably the non functioning has something to do with a server too busy, or so. But as the program doesn't get an error code the user has to repeat the request manually, whereas I would like to retry progammatically using a timer.

The map loading part of the program is very simple:
Freeze various timers used in the program
WEB1.Navigate(URLStr)

Sub WEB1_DocumentCompleted
Unfreeze various timers

Any suggestion to solve my problem is welcomed.

Regards,
Harry
 

mjcoon

Well-Known Member
Licensed User
... Why browser anyway, why not in an image object?

I'm a bit curious why you say this. Surely what you get back from the HTTP request is a string of HTML in which the data for the bitmap is embedded. This data has to be separated out from the rest of the HTML before it can be put in an image. The browser knows how to do that. But no doubt you could do the parsing and extraction separately if you wished...

Mike (who only knows a little bit about HTTP/HTML).
 

HARRY

Active Member
Licensed User
Longtime User
Mike,

Could you show me how to get the map (image) resulting from the HTTP request into the WEBbrowser, without reading the map again?

If the WEBBrowser analyses the result from the request where does it leave error information?

Harry
 

mjcoon

Well-Known Member
Licensed User
Mike,

Could you show me how to get the map (image) resulting from the HTTP request into the WEBbrowser, without reading the map again?

If the WEBBrowser analyses the result from the request where does it leave error information?

Harry

Sorry, I can't answer either of those questions, but find it interesting enough to take a look.

Meanwhile Andrew Graham may pop up with an analysis.

Mike.
 

mjcoon

Well-Known Member
Licensed User
Could you show me how to get the map (image) resulting from the HTTP request into the WEBbrowser, without reading the map again?

On 2nd thoughts, I'm not sure what you mean by "again".

My reading of the Help for the two DLLs is that you can do
B4X:
WebBrowser.DocumentText = Response.GetString

But of course if you want to do the initial navigation using WebBrowser then using HTTP.dll as well would imply two fetches. I gather WebBrowser.DocumentText is a write-only property (though note that the use of vs. [O] in the Help is opposite to some others).

This is what Basic4Life was referring to:
...it might be possible to do all the downloading with the HTTP lib and use the webbrowser only to display the data.
HTH, Mike.
 

HARRY

Active Member
Licensed User
Longtime User
Mike,

Now I understand the use of .Getstring. I added the following code :
in Case 200
code = "OK"
browser.DocumentText=responseCheck.GetString

However, indeed a string is displayed in the WEB Control, not an image. So, apparantly more should be done .

Does somebody know what?

Harry
 

mjcoon

Well-Known Member
Licensed User
Hi Mike,

No, I guess that the string are the bytes of a compressed image file, at least the first 9 bytes are: ����JFIF

Harry

Ah, it's might be worth trying to write that data to a file called ....jpg to see if it can be read as if an image. (If it could, I think there's an interface that could show it without being written out 1st.)

I also note that there is Response.GetStream as well, which unfortunately writes to file, but it is not clear from the Help whether the data is different beyond being treated as bytes rather than characters.

Mike.
 

HARRY

Active Member
Licensed User
Longtime User
Hi mike,

Bingo, it works now very well with the following code:

At App Start:
AddObject("MapBmp","BitmapEx")
MapBmp.New2(1,1)
AddObject("DrawerMapBmp","DrawerEx")
DrawerMapBmp.New1("FollowForm",False)
AddObject("RectSrc","RectangleEx")
AddObject("RectDes","RectangleEx")
RectSrc.New1(120*FixX,120*FixY,FixX*240,FixY*240)
RectDes.New1(0,0,240*FixX,240*FixY)
AddObject("MapRequest","WEBRequest")
AddObject("MapResponse","WEBResponse")
MapResponse.New1


Each time to read a map:
MapRequest.New1(URLStr)
MapRequest.TimeOut=30000
MapResponse.Value=MapRequest.GetResponse
If MapRequest.ResponseCode= 200 Then
MapBmp.Release
Reader.New1(MapResponse.GetStream,True) 'Use a BinaryFile object to read the data from the Response stream.
If FileExist(AppPath & "\MapFile.jpg") = True Then
FileDel(AppPath & "\MapFile.jpg")
End If
FileOpen(c1,AppPath & "\MapFile.jpg",cRandom)
Writer.New1(c1,False)
Dim buffer(4096) As byte
count = Reader.ReadBytes(buffer(),4096)
Do While count > 0
Writer.WriteBytes2(buffer(),0,count)
count = Reader.ReadBytes(buffer(),4096)
Loop
FileClose(c1)
MapBmp.Create1(AppPath & "\MapFile.jpg")
DisplayMap (0,0)

Sub DisplayMap(x,y)
RectSrc.New1(X,Y,240*FixX , 240*FixY ) DrawerMapBmp.Drawimage(MapBmp.Value,RectSrc.value,RectDes.value,False)
DrawerMapBmp.RefreshForm2("FollowForm",RectDes.value)
End Sub

Thanks for your help and suggestions.

Harry
 
Top