简体   繁体   English

用于清理用户输入的URL的Delphi代码

[英]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. 我需要用户能够输入URL,并希望确保它尽可能健康。 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). 比如检查前面有http://,没有双点,也许是有效的TLD,尾随斜线(我必须添加最后一页)。

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. [编辑:]为了清楚起见,这是Windows服务中的运行时要求。 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. 目的是从配置中读取的URL中获得最佳效果,而不是验证用户输入的内容。实质上,如果我可以调整URL并使其工作,那么这就是我想要做的。 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? 如何在Windows API中使用PathIsURL函数?

Update: This is already wrapped in the Delphi RTL in the ShLwApi unit. 更新:这已经包含在ShLwApi单元中的Delphi RTL中。

Have you had a look at " What is the best regular expression to check if a string is a valid URL "? 您是否看过“ 检查字符串是否为有效URL的最佳正则表达式是什么 ”? It is not Delphi specific, but might get you started. 它不是Delphi特有的,但可能会让你入门。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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