Strange behavior with TextField and URLConnection URL

In a desktop application that have a Button and a TextField:

in the button, I pass an URL variable (String) to URLConnection to download a file.

After I add a TextField and pass TextField1.Text to URLConnection, nothing is downloaded.
When I use the same string in code… it works !

I do not understand what happens (no special character; only a…z and %é0 for spaces; per original URL).
Even when I copy TextField1.Text to Firefox, the file is downloaded.

Asking for a project is asking for the whole project: the only code is in a button, some lines only;

Ideas ?

Asking for a project is for you to create a simple/small/new project with only the textfield, button and URLConnection code, so others can see your code in action and give you exact information.

1 Like

Is that a typo? Spaces should be encoded as %20 not %é0

1 Like

Have you breakpointed the debugger and examined the binary content of the string being passed to URLconnection? TextField accepts line endings, and users have a habit of hitting Enter or Return after entering text, so maybe you are passing a line ending. Try using TextField1.Text.Trim.

2 Likes

Yes, typo: in my AZERTY keyboard, I need to press Shif-é to get the character ‘2’. Sorry.

@Alberto: I know exactly what you mean, but there is nothing to test here.

the relevant code is:

imageURL = TF_URL.Text
content = connect.SendSync(“GET”, theURL, timeoutInSeconds)

TF_URL is a TextField
imageURL is a String
connect is a URLConnection Class
content is a MemoryBlock
timeoutInSeconds is an Integer (= 15)

The URL comes from the Finder / a Copy / Paste, and a month (or year/month) change, but the same paste is used to make the test.

The code that works is when I paste the URL in the code instead of in the TextField (TF_URL)

The whole project is around 10 lines. [a computer crash occured].

Ideas ?

Or must I continue to run / quit, change the date and so on ?

What is: “theURL”? or is a typo and you mean “imageURL”?

That is why a small sample project helps a lot.

To follow up on what @Julia_Truchsess says:

This is the best way to verify you don’t have garbage in your string variable.

1 Like

Thatis what I usually do when I read file names from macOS, but in that case …

I copied an URL (as text) and paste it in both the TextField and the code Editor button:
in one case (Paste in the Code Editor), the file is downloaded,
in the other case (Paste in TextField) it is not downloaded.

I will upload the project in some minutes.

As @AlbertoD already mentioned. Should’nt i be more like:

imageURL = TF_URL.Text.Trim.ReplaceAll(" ", "%20")
content = connect.SendSync(“GET”, imageURL, timeoutInSeconds)

I was suspicious, and the facts proved I as right:

the example works fine ! and I do not understand why.

27 lines comments included.

I will try with a different URL just in case…

Worst (or far better): I was able to get a file when yesterday it was returning an error.

OK: sorry for the noise.

That is what I have for saving the pdf on disk.

I have ten thousand of pdfs downloaded with an URL that have %20 in it.

BTW: this is a typo, the original line (without the TextField) used theURL. Of course if I mismatch variable it will never work at all (in the two cases)…

Case solved by itself (!)

Thank you all.

The bug beat me some minutes ago.

So I searched the example, fired it, pasted the “offending” URL and… it was downloaded.

So, I checked the Example code and see a difference with mine:
.SendSync for my code vs .Send for the example.

I changed my code to use the URL that does not downloads previously, and that worked (!!!)

So, there is something, but I do not know what.

PS: I add:
System.DebugLog connect.HTTPStatusCode.ToString

after the "GET" line, but it never appears…

LBN: I run the project with final changes (*) and nothing happened… so I drink a glass of fresh water and come back to see an OS dialog asking for write permission (!)…

(*): I select a “folder” path, then in a loop, I add the pdf file name to the string and download that. Worked fine for many folders, but some. Each time I can’t, I check the path for some files for that folder and if rejected, I go to the next folder (a folder in this case holds a year as digits - 1940 for example - and the first three letters for the month - Jan, Feb, etc. - ).

The HTTPStatusCode is 0.
BUT: Request url is not well-formed.

I have to investigate.

The url is:
https://fultonhistory.com/Newspapers 23/Freeport NY Daily Review/Freeport NY Daily Review 1942 Jan/Freeport NY Daily Review 1942 Jan - 0011.pdf

Download fine with FireFox. I copy the url from the variable passed with “GET”, in the debuger…

You probably have to replace all the spaces in the link with %20.

1 Like

More generally, you need to Encode the URL:

Also, the browser can behave differently than URLConnection.

Things like browser detection on the server or following redirections some things that may be affecting your app.

In fact, some browsers will do the URL Encoding for you - if you paste a URL containing characters that need to be encoded (such as spaces, etc.) it will auto-encode it for you.

In other words, pasting a URL into a browser is not a valid way to ensure your URL will work with URLConnection.

For example, with Safari, what happens if you paste or type various strings into the search field?

example.com/foo bar gets treated as a Google search.

https://example.com/foo bar will be automatically URL encoded into
https://example.com/foo%20bar