Empty return from http page

junglejet

Active Member
Licensed User
Longtime User
I use the "standard" http download routine to upload parameters in the url string. The server may return an empty string which is ok. However the routine throws an exception and ends behind the errorlabel. Can that be avoided and empty strings handled as such? (Device only)

B4X:
Sub GetText (URL)
   ErrorLabel(eURL)
      Response.New1
      Request.New1(URL)
      Response.Value = Request.GetResponse 'This line calls the server and gets the response.
      string = Response.GetString 'Get the Response string.
      Response.Close
      Return string
    eUrl:
     If buStatus.Color=cGreen Then buStatus_Click 'Stops Auto mode
     Msgbox("Connection error","")
     Return
End Sub
 
Last edited:

digitaldon37

Active Member
Licensed User
Longtime User
I use the "standard" http download routine to upload parameters in the url string. The server may return an empty string which is ok. However the routine throws an exception and ends behind the errorlabel. Can that be avoided and empty strings handled as such? (Device only)

B4X:
Sub GetText (URL)
   ErrorLabel(eURL)
      Response.New1
      Request.New1(URL)
      Response.Value = Request.GetResponse 'This line calls the server and gets the response.
      string = Response.GetString 'Get the Response string.
      Response.Close
      Return string
    eUrl:
     If buStatus.Color=cGreen Then buStatus_Click 'Stops Auto mode
     Msgbox("Connection error","")
     Return
End Sub

Have you tried returning the variable string on an error?
B4X:
 eUrl:
     If buStatus.Color=cGreen Then buStatus_Click 'Stops Auto mode
     Msgbox("Connection error","")
     Return string
String will be empty, but the variable that you are setting with the GetText call will have a value set.
 

junglejet

Active Member
Licensed User
Longtime User
Yes, I could ignore the exception, but there are also valid exceptions when thre is a 404 or so. That's why I prefer to correctly have the empty string processed.
 

digitaldon37

Active Member
Licensed User
Longtime User
Yes, I could ignore the exception, but there are also valid exceptions when thre is a 404 or so. That's why I prefer to correctly have the empty string processed.

To me that looks like you are getting the error thrown in main when you call GetText and do not get a return string. If your code is this:
B4X:
html=getText(url)

and if GetText doesn't return anything (which I believe is what is happening - others can jump in and correct me - because your code exits with "return" instead of "return string") then it seems to throw an error on main.

I think a 404 error would still return a string and not hit your error code. I think the error code is only triggered if the http call gets nothing (like on a time-out)

I've had good luck always returning the variable string in GetText and then passing the return value to another sub for evaluation.
 

agraham

Expert
Licensed User
Longtime User
Yes, I could ignore the exception, but there are also valid exceptions when thre is a 404 or so. That's why I prefer to correctly have the empty string processed.
Perhaps you could use my http://www.b4x.com/forum/additional-libraries/2305-exceptions-handling-library.html#post12486 in your Errorlabel code block to see what the error actually is and decide what to do.

- because your code exits with "return" instead of "return string" then it seems to throw an error on main.
As a technical detail just "return" by itself, and also running off the end of a Sub both return an empty string as in - return ""
 
Last edited:

junglejet

Active Member
Licensed User
Longtime User
The exception is definitely thrown in GetText, not the caller. An empty string reply is allowed in http protocol together with reply code 200. Every browser and the B4PPC IDE do recognize this as valid. The device (optimized) does not. I believe Andrew's proposal is best, though I believe the exceptions.dll cannot be merged?
 

digitaldon37

Active Member
Licensed User
Longtime User
The exception is definitely thrown in GetText, not the caller. An empty string reply is allowed in http protocol together with reply code 200. Every browser and the B4PPC IDE do recognize this as valid. The device (optimized) does not. I believe Andrew's proposal is best, though I believe the exceptions.dll cannot be merged?

I wasn't suggesting that the http was throwing an exception, I was wondering if not setting a return string was causing it.

You are right, if it works in a browser then it should work in the B4PPC http library as well.
 

agraham

Expert
Licensed User
Longtime User
Top