C# 无法将 System.String 转换为 System.Uri

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1465684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:49:59  来源:igfitidea点击:

Cannot Convert System.String to System.Uri

c#flickr

提问by dezkev

I am using the Web Client Class to download files from the internet (Flickr actually). This works fine as long as I use : WebClient().DownloadData(string), however this locks up the UI as it is Not asynchronous.

我正在使用 Web 客户端类从 Internet 下载文件(实际上是 Flickr)。只要我使用 : 就可以正常工作WebClient().DownloadData(string),但是这会锁定 UI,因为它不是异步的。

However when I try WebClient().DownloadDatAsync(string), I get a compile error: "Unable to convert System.String to System.Uri".

但是,当我尝试时WebClient().DownloadDatAsync(string),出现编译错误:“无法将 System.String 转换为 System.Uri”。

The string MediumUrl returns "http://farm4.static.flickr.com/2232/2232/someimage.jpg"

字符串 MediumUrl 返回 "http://farm4.static.flickr.com/2232/2232/someimage.jpg"

So the question is how do I convert the string "http://farm4.static.flickr.com/2232/2232/someimage.jpg"to a Uri.

所以问题是如何将字符串转换"http://farm4.static.flickr.com/2232/2232/someimage.jpg"为 Uri。

Things I have tried-

我尝试过的事情-

  1. I have tried to cast it to Uri but that does not work either.
  2. I have tried Uri myuri = new uri(string)- errors out as above.

    foreach (Photo photo in allphotos)  
    {  
        //Console.WriteLine(String.Format("photo title is :{0}", photo.Title));
        objimage = new MemoryStream(wc.DownloadData(photo.MediumUrl));
        images.Add(new Pictures(new Bitmap(objimage), photo.MediumUrl, photo.Title));  
    }
    
  1. 我试图将它转换为 Uri 但这也不起作用。
  2. 我试过Uri myuri = new uri(string)- 错误如上。

    foreach (Photo photo in allphotos)  
    {  
        //Console.WriteLine(String.Format("photo title is :{0}", photo.Title));
        objimage = new MemoryStream(wc.DownloadData(photo.MediumUrl));
        images.Add(new Pictures(new Bitmap(objimage), photo.MediumUrl, photo.Title));  
    }
    

回答by Thomas Levesque

objimage = new MemoryStream(wc.DownloadData(new Uri(photo.MediumUrl)));

b) I have tried Uri myuri = new uri(string) - errors out as above.

b) 我试过 Uri myuri = new uri(string) - 上面的错误。

This is the usual way to create a Uri from a string... I don't see why it wouldn't work if the string is a valid URI

这是从字符串创建 Uri 的常用方法......如果字符串是有效的 URI,我不明白为什么它不起作用

回答by Daniel Elliott

var yourUri = new UriBuilder(yourString).Uri;

So your example would be:

所以你的例子是:

wc.DownloadDataAsync(new UriBuilder(photo.MediumUrl).Uri);
objimage = new MemoryStream(wc.Result);

You may need to put a check in to see the operation has completed.

您可能需要签入以查看操作已完成。

Hope that helps,

希望有帮助,

Dan

回答by Mitch Wheat

If I understand your code correctly, then

如果我正确理解你的代码,那么

wc.DownloadDataAsync(new Uri(photo.MediumUrl));

should work.

应该管用。

回答by Digitalex

This works just fine;

这很好用;

System.Uri uri = new System.Uri("http://farm4.static.flickr.com/2232/2232/someimage.jpg");

By the way; I notice you mistyped the expression new uri(..., with lowercase uri. This is not your problem, is it? Because it should be "new Uri".

顺便一提; 我注意到你错误地输入了表达式 new uri(..., 用小写的 uri。这不是你的问题,是吗?因为它应该是“new Uri”。

回答by Peter Kelly

Okay, so I think if the others have proved your URI is valid in their code and it compiles etc. and you also mention it is generated at runtime - it could be that the UriString you are generating at runtime is invalid and not what you are expecting?

好的,所以我认为如果其他人已经证明您的 URI 在他们的代码中有效并且可以编译等,并且您还提到它是在运行时生成的 - 可能是您在运行时生成的 UriString 无效,而不是您的身份期待?

Instead of letting an exception be thrown for attempting to create a Uri from an invalid string, I would suggest the following method IsWellFormedUriStringon the Uriclass.

我不会因为尝试从无效字符串创建 Uri 而抛出异常,而是建议在Uri类上使用以下方法IsWellFormedUriString

string uriString = "your_UriString_here";

if (Uri.IsWellFormedUriString(uriString, UriKind.Absolute))
{
    Uri uri = new Uri(uriString);
}
else
{
    Logger.WriteEvent("invalid uriString: " + uriString);
}

Might help with your debugging also.

也可能有助于您的调试。