1. Roy Bungert
  2. PowerBuilder
  3. Wednesday, 5 May 2021 13:31 PM UTC

Hello,

I have the following url (only an example):

url = www.google.de/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png 

I want to know whether this picture exists or not. If it doesn't exist I want to get something like error "404 Not found"

Any ideas?

 

Regards

Roy

Accepted Answer
Daryl Foster Accepted Answer Pending Moderation
  1. Thursday, 6 May 2021 01:26 AM UTC
  2. PowerBuilder
  3. # Permalink

Hi Roy,

Here is some code that you can use as the basis of a function to check a URL.  I've just added some comments where you would tailor it to your specific needs, depending on whether you just need a function with an Exists/Not Exists/Error status, or something more complex.

It uses a HEAD http request which is basically a GET request that doesn't return the content.  Most servers should support it, but if not it can be replaced with a GET request.  The GET request will return the file contents, so it will be slower than a HEAD request.

integer li_response = -1
integer li_rc
string ls_content_length
string ls_url

// this should be the argument to the function
ls_url = 'www.google.de/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png'

// The url needs to begin with http or httpd, so add it if it doesn't
if Lower(Left(ls_url, 4)) <> 'http' then ls_url = 'https://' + ls_url

HttpClient lo_httpclient
lo_httpclient = Create HttpClient

// If the server doesn't support HEAD, you can use GET, but then the whole file is downloaded
li_rc = lo_httpclient.SendRequest('HEAD', ls_url)

if li_rc = 1 then
    li_response = lo_httpclient.GetResponseStatusCode()

    choose case li_response
        case 200
            // Found.  Can set a status or do something else here if required
            
            // You can get the content length if you need to see how big the file is
            ls_content_length = lo_httpclient.GetResponseHeader('content-length')
        case 404
            // Not Found.  Can set a status or do something else here if required
        case else
            // Some Other Error.  Can set a status or do something else here if required
    end choose
else
    // An error with the request (e.g. invalid server).
    // Can set a status or do something else here if required
    // MessageBox('Error Checking', 'Error calling ' + ls_URL + '~r~nReturn Code ' + string(li_rc))
end if
destroy lo_httpclient

return li_response

 

 

Comment
  1. Roy Bungert
  2. Thursday, 6 May 2021 08:29 AM UTC
Hello Daryl, thank you very much. It works perfectly!
  1. Helpful
  1. Daryl Foster
  2. Thursday, 6 May 2021 22:45 PM UTC
No worries Roy, glad I could help.
  1. Helpful
There are no comments made yet.
Julie Jiang @Appeon Accepted Answer Pending Moderation
  1. Thursday, 6 May 2021 07:53 AM UTC
  2. PowerBuilder
  3. # 1

Hi Chris, 

iNET object was claimed as obsolete starting from PowerBuilder 2019 R3. We planned not to enhance it further. Part of the reason is that it is stable enough. And the other part is that we want to encourage users to switch to WebBrowser. 

The last enhancement/bug fix we made to iNET was in PowerBuilder 2019 MR 2170 and PowerBuilder 2017 R3 MR 1915. After that, no further changes to it.

Best regards, Julie

Comment
  1. Armeen Mazda @Appeon
  2. Friday, 7 May 2021 15:07 PM UTC
Hi Chris, We are all on the same page that the INet object has valuable features and is used a lot. There are complex reasons behind why the product group decided to make this obsolete, which Julie alluded to in her post. Per the product management notice, the migration path is to use the WebBrowser control: https://www.appeon.com/developers/obsolete-features-in-powerbuilder-2019R3.html Maybe you can build this in as a feature of your open-source framework?
  1. Helpful
  1. Chris Pollach @Appeon
  2. Friday, 7 May 2021 15:15 PM UTC
Hi Armeen ... I have recently thought of that alternative BUT .. you have to remember that many PB Apps are non-visually - for example: run as a batch, service or just called behind the scenes to perform HTTP work silently. In order to use the Web Browser control, you will need a visual based App whereas, we can currently use the iNET or HTTPClient non-visually. Also the memory overhead of the INET / HTTPClient vs the Web Browser control - memory & CPU wise.

Food for thought.
  1. Helpful
  1. Armeen Mazda @Appeon
  2. Friday, 7 May 2021 15:35 PM UTC
Yes, there is no doubt that this migration path is not going to be ideal for all situations, especially if the app doesn't need to use the WebBrowser control for other things. Is there a way you can add this to your framework that maps to Windows API calls to do the HyperLinkToURL function?
  1. Helpful
There are no comments made yet.
Mark Goldsmith Accepted Answer Pending Moderation
  1. Thursday, 6 May 2021 01:20 AM UTC
  2. PowerBuilder
  3. # 2

Hi Roy,

You could use the WebBrowser control (not for the return value on the Navigate() call as it too seems to return a 1 regardless, unless that's what you were already referring to but see my reply to Chris' post for my thoughts on return values) and then use GetSource() and search for whether the string returned contains the text "Error 404". Clunky, but should accomplish what you want.

Just be careful not to issue GetSource() too soon as it takes a second to populate and the content may not have made it into the WebBrowser control yet.

HTH...regards,

Mark

Comment
There are no comments made yet.
Daryl Foster Accepted Answer Pending Moderation
  1. Thursday, 6 May 2021 00:34 AM UTC
  2. PowerBuilder
  3. # 3

Hi Roy, do you want to download the file, or just check whether it exists?  Do you want it for any URL, or specifically images?

Comment
There are no comments made yet.
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Wednesday, 5 May 2021 15:01 PM UTC
  2. PowerBuilder
  3. # 4

Hi Roy;

   Just a thought  .... Have you tried the PB iNET object class's GetURL() command and check if it returns a -2 RC (not found) to see if that works for your application?

Regards ... Chris

Comment
  1. Armeen Mazda @Appeon
  2. Thursday, 6 May 2021 16:30 PM UTC
@Chris, You are entitled to your opinion, but we should make sure customers do not misunderstand. The INet object is obsolete and Appeon is NOT enhancing it. Customers using obsolete feature are on their own.

  1. Helpful
  1. Mark Goldsmith
  2. Thursday, 6 May 2021 17:06 PM UTC
Thanks Chris and I agree that a IsURLResourceValid() command would provide what's needed. I'm still curious though how the Navigate() call would ever return a -5 as anytime I would have expected it to it doesn't. That's why I was wondering whether it was working properly. If it's not and a fix could be provided then that would obviate the need for something like IsURLResourceValid().



Daryl's solution is great and does what is needed but feels like it's additional code that wouldn't be necessary if Navigate() returned a -5 when a 404 is returned...just my thoughts.



Regards,



Mark
  1. Helpful
  1. Daryl Foster
  2. Thursday, 6 May 2021 23:10 PM UTC
Hi Chris,



This is probably one of the few times I've disagreed with something you've said. I don't think that HyperLinkToURL() or even IsURLResourceValid() should be added to the HttpClient object. From what I understand the HttpClient is an implementation of the Http protocol so it's lower level than either of those two functions. I've never used HyperLinkToURL(), but doesn't it just pass a URL to the system browser and then the browser is responsible for making the call. It seems like that is completely outside the scope of the HttpClient. Similarly I think an IsURLResourceValid() function is a much higher level function which could be implemented using the HttpClient, but not be one of it's methods. I agree that you probably do need both of those functions, but they are probably better either as separate functions, or as methods of a separate HTTPUtilities object rather than bloating the HttpClient object.



Regards,

Daryl.
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.