【问题标题】:C# check if a string is a valid WebProxy formatC# 检查字符串是否是有效的 WebProxy 格式
【发布时间】:2014-06-07 18:47:28
【问题描述】:
所以在这里和谷歌搜索后,我一直无法找到解决方案。基本上我想允许用户从文本文件中添加代理列表,但我想在创建 WebProxy 之前检查传入的代理格式是否有效。我知道使用 try {var proxy = new WebProxy(host + ":" + port;} catch{} 之类的 try catch 会起作用,但您可能已经知道使用 try catch 很慢,批量执行时更是如此。
那么,什么是测试字符串以查看它是否采用有效的 WebProxy 格式的又好又快的方法呢?
谢谢
【问题讨论】:
标签:
c#
httpwebrequest
webproxy
【解决方案1】:
这就是我的做法:
var ValidIpAddressPortRegex = @"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]):[\d]+$";
if (System.Text.RegularExpressions.Regex.IsMatch(proxy_string_to_validate, ValidIpAddressPortRegex ))
{
do_stuff_with_proxy();
}
else
{
//MessageBox.Show("IP:PORT url not valid!","Information");
}