C# 不支持 URL 格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17918433/
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-10 10:48:53  来源:igfitidea点击:

URL Formats are not supported

c#asp.net

提问by Syed Salman Raza Zaidi

I am reading File with File.OpenRead method, I am giving this path

我正在使用 File.OpenRead 方法读取文件,我给出了这个路径

   http://localhost:10001/MyFiles/folder/abc.png

I have tried this as well but no luck

我也试过这个,但没有运气

http://localhost:10001//MyFiles//abc.png

but its giving

但它的给予

URL Formats are not supported

不支持 URL 格式

When I give physical path of my Drive like this,It works fine d:\MyFolder\MyProject\MyFiles\folder\abc.png

当我像这样给出我的驱动器的物理路径时,它工作正常 d:\MyFolder\MyProject\MyFiles\folder\abc.png

How can I give file path to an Http path?

如何为 Http 路径提供文件路径?

this is my code

这是我的代码

public FileStream GetFile(string filename)
{
    FileStream file = File.OpenRead(filename);
    return file;
}

采纳答案by philipproplesch

You could either use a WebClient as suggested in other answers or fetch the relative path like this:

您可以按照其他答案中的建议使用 WebClient,也可以像这样获取相对路径:

var url = "http://localhost:10001/MyFiles/folder/abc.png";

var uri = new Uri(url);
var path = Path.GetFileName(uri.AbsolutePath);

var file = GetFile(path);
// ...

In general you should get rid of the absolute URLs.

一般来说,您应该摆脱绝对 URL。

回答by Ross McNab

Have a look at WebClient(MSDN docs), it has many utility methods for downloading data from the web.

看看WebClientMSDN 文档),它有许多从网络下载数据的实用方法。

If you want the resource as a Stream, try:

如果您想要资源作为Stream,请尝试:

using(WebClient webClient = new WebClient())
{
    using(Stream stream = webClient.OpenRead(uriString))
    {
        using( StreamReader sr = new StreamReader(stream) )
        {
            Console.WriteLine(sr.ReadToEnd());
        }
    }
}

回答by Bart Friederichs

HereI found this snippet. Might do exactly what you need:

在这里我找到了这个片段。可能完全符合您的需要:

using(WebClient client = new WebClient()) {
   string s = client.DownloadFile(new Uri("http://.../abc.png"), filename);
}

It uses the WebClientclass.

它使用WebClient类。

回答by Kamil Solecki

The best way to download the HTML is by using the WebClient class. You do this like:

下载 HTML 的最佳方式是使用 WebClient 类。你这样做:

    private string GetWebsiteHtml(string url)
    {
        WebRequest request = WebRequest.Create(url);
        WebResponse response = request.GetResponse();
        Stream stream = response.GetResponseStream();
        StreamReader reader = new StreamReader(stream);
        string result = reader.ReadToEnd();
        stream.Dispose();
        reader.Dispose();
        return result;
    }

Then, If you want to further process the HTML to ex. extract images or links, you will want to use technique known as HTML scrapping.

然后,如果你想进一步处理 HTML 到 ex。提取图像或链接,您将需要使用称为 HTML 抓取的技术。

It's currently best achieved by using the HTML Agility Pack.

目前最好通过使用HTML Agility Pack 来实现

Also, documentation on WebClient class: MSDN

此外,关于 WebClient 类的文档:MSDN