简体   繁体   中英

Delphi code for sanitizing a URL entered by the user

I need a user to be able to enter a URL, and would like to make sure it is as wholesome as possible. Things like checking that there is http:// at the front, no double-dots, perhaps valid TLD, trailing slash (I have to add the final page).

I figure this is such a common requirement that it must exist already. Suggestions?

[edit:] To be clear, this is a run-time requirement in a Windows Service. The aim is to get the best from the URL read from the configuration, rather than validate what the user typed in. In essence, if I can adjust the URL and make it work, then that is what I'd like to do. The download will be a specific file, so if it all goes wrong it won't get the wrong thing from another server by mistake.

How about using the PathIsURL function in the Windows API?

Update: This is already wrapped in the Delphi RTL in the ShLwApi unit.

Have you had a look at " What is the best regular expression to check if a string is a valid URL "? It is not Delphi specific, but might get you started.

也许这里的一些建议可能会有所帮助。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM