- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在构建一个应用程序以从 Internet 检索图像。尽管它工作正常,但在应用程序中使用 try-catch 语句时速度很慢(在错误的给定 URL 上)。
(1) 这是验证 URL 和处理错误输入的最佳方式吗?还是我应该改用 Regex(或其他一些方法)?
(2) 如果我没有在文本框中指定 http://,为什么应用程序会尝试在本地查找图像?
private void btnGetImage_Click(object sender, EventArgs e)
{
String url = tbxImageURL.Text;
byte[] imageData = new byte[1];
using (WebClient client = new WebClient())
{
try
{
imageData = client.DownloadData(url);
using (MemoryStream ms = new MemoryStream(imageData))
{
try
{
Image image = Image.FromStream(ms);
pbxUrlImage.Image = image;
}
catch (ArgumentException)
{
MessageBox.Show("Specified image URL had no match",
"Image Not Found", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
catch (ArgumentException)
{
MessageBox.Show("Image URL can not be an empty string",
"Empty Field", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
catch (WebException)
{
MessageBox.Show("Image URL is invalid.\nStart with http:// " +
"and end with\na proper image extension", "Not a valid URL",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
} // end of outer using statement
} // end of btnGetImage_Click
编辑:我尝试了 Panagiotis Kanavos 建议的解决方案(感谢您的努力!),但如果用户输入 http://
仅此而已,它只会被 if-else 语句捕获。更改为 UriKind.Absolute 也会捕获空字符串!越来越近 :)目前的代码:
private void btnGetImage_Click(object sender, EventArgs e)
{
String url = tbxImageURL.Text;
byte[] imageData = new byte[1];
Uri myUri;
// changed to UriKind.Absolute to catch empty string
if (Uri.TryCreate(url, UriKind.Absolute, out myUri))
{
using (WebClient client = new WebClient())
{
try
{
imageData = client.DownloadData(myUri);
using (MemoryStream ms = new MemoryStream(imageData))
{
imageData = client.DownloadData(myUri);
Image image = Image.FromStream(ms);
pbxUrlImage.Image = image;
}
}
catch (ArgumentException)
{
MessageBox.Show("Specified image URL had no match",
"Image Not Found", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
catch (WebException)
{
MessageBox.Show("Image URL is invalid.\nStart with http:// " +
"and end with\na proper image extension",
"Not a valid URL",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
else
{
MessageBox.Show("The Image Uri is invalid.\nStart with http:// " +
"and end with\na proper image extension", "Uri was not created",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
我一定是做错了什么。 :(
最佳答案
使用Uri.TryCreate仅当您的 url 字符串是有效 URL 时才创建新的 Uri 对象。如果该字符串不是有效的 URL,则 TryCreate 返回 false。
string myString = "http://someUrl";
Uri myUri;
if (Uri.TryCreate(myString, UriKind.RelativeOrAbsolute, out myUri))
{
//use the uri here
}
更新
TryCreate 或 Uri 构造函数将愉快地接受可能看起来无效的字符串,例如“Host: www.stackoverflow.com”、“Host:%20www.stackoverflow.com”或“chrome:about”。事实上,这些是完全有效的 URI,它们指定了自定义方案而不是“http”。
Uri.Scheme 的文档property 提供了更多示例,例如“gopher:”(有人记得这个吗?)、“news”、“mailto”、“uuid”。
应用程序可以将自己注册为自定义协议(protocol)处理程序,如 MSDN 中所述或其他 SO 问题,例如 How do I register a custom URL protocol in Windows?
TryCreate 不提供将自身限制为特定方案的方法。代码需要检查 Uri.Scheme 属性以确保它包含可接受的值
更新 2
传递一个奇怪的字符串,如 "></script><script>alert(9)</script>
将返回 true
并构造一个相对的 Uri 对象。来电 Uri.IsWellFormedOriginalString虽然会返回 false。所以你可能需要调用IsWellFormedOriginalString
如果您想确保相对 Uris 的格式正确。
另一方面,调用 TryCreate
与 UriKind.Absolute
在这种情况下将返回 false。
有趣的是,Uri.IsWellFormedUriString 在内部调用 TryCreate,然后返回 IsWellFormedOriginalString
的值如果创建了相对 Uri。
关于c# - 在 C# 中验证 URL 的一种比 try-catch 更好的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3228984/
我刚刚遇到了一个非常奇怪的行为。这是代码: // So far everything's fine val x: Try[Try[Unit]] = Try(Try{}) x: scala.util.T
“输出”是一个序列化的 OpenStruct。 定义标题 try(:output).try(:data).try(:title) 结束 什么会更好? :) 最佳答案 或者只是这样: def title
我有以下元组 - (t1,t2) :(Try,Try) 我想检查两者是否成功或其中之一是否失败,但避免代码重复。像这样的东西: (t1,t2) match { case (Success(v1),Su
是否必须放置内部 try-with-resources 或其中一个 try-with-resources 中的所有内容都会自动关闭? try (BasicDataSource ds = Bas
有一点特殊,尝试创建一段 try catch 代码来处理 GoogleTokenResponse,但编译器在 try 时抛出异常错误。有什么想法吗? 错误信息: | Loading Grails 2.
它几乎可以在所有语言中找到,而且我大部分时间都在使用它。 我不知道它是内部的,不知道它是如何真正起作用的。 它如何在任何语言的运行时在 native 级别工作? 例如:如果在 try 内部发生 sta
为什么在 readFile2() 中我需要捕获 FileNotFoundException 以及稍后由 close( ) 方法,并且在 try-with-resources(inside readfi
我正在使用 Apache POI 尝试读取 Word 文件,但即使您使用过 Apache POI,这仍然应该是可以回答的。在 HWPF.extractor 包中有两个对象:WordExtractor
如果try-catch的catch block 中抛出异常,那么finally block 会被调用吗? try { //some thing which throws error } cat
这个问题已经有答案了: What's the purpose of try-with-resources statements? (7 个回答) 已关闭 3 年前。 我一直在查看代码,并且已经看到了对
这个问题已经有答案了: What's the purpose of try-with-resources statements? (7 个回答) 已关闭 3 年前。 我一直在查看代码,并且已经看到了对
我正在使用 Try::Tiny尝试捕捉。 代码如下: use Try::Tiny; try { print "In try"; wrongsubroutine(); # undefi
我想知道这样的代码是否会在抛出异常后总是中断而不继续运行,因此代码不会继续执行第二个 temp.dodaj(b)。 Avto *a = new Avto("lambo",4); Avt
我知道在try子句中必须有一个与资源关联的变量声明。 但是除了被分配一个通常的资源实例化之外,它是否可以被分配一个已经存在的资源,例如: public String getAsString(HttpS
我有一个写的方法。此方法仅扫描用户输入的整数输入。如果用户输入一个字符值,它将抛出一个输入不匹配异常,这是在我的 Try-Catch 语句中处理的。问题是,如果用户输入任何不是数字的东西,然后抛出异常
我注意到这不会编译: PrintWriter printWriter = new PrintWriter("test.txt"); printWriter.append('a'); printWrit
我经常看到人们写这样的代码: try: some_function() except: print 'something' 当我认为这样做更干净时: try: some_functio
该应用程序将在第二个显示器上正常显示内容。问题是当我旋转 iPad 时内容不会在 iPad 上旋转。 看过: http://developer.apple.com/library/ios/#qa/qa
我正在学习 java,我发现我不喜欢的一件事通常是当我有这样的代码时: import java.util.*; import java.io.*; public class GraphProblem
我使用 C++ 有一段时间了,对普通的 try/catch 很熟悉。但是,我现在发现自己在 Windows 上,在 VisualStudio 中编码以进行 COM 开发。代码的几个部分使用了如下内容:
我是一名优秀的程序员,十分优秀!