如何使用StreamReader从URI读取文件? [英] How to read a file from a URI using StreamReader?

查看:65
本文介绍了如何使用StreamReader从URI读取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要使用StreamReader读取的URI文件.显然,这会导致问题,因为File.OpenText不支持URI路径.该文件是其中包含一堆html的txt文件.我有多个使用同一段html的网页,因此我将其放在txt文件中,并在页面加载时将其读入页面中(将文件放在文件系统中时我可以使用它,但需要将其在线放置在文档库中,以便业务用户可以使用它).我试图避免使用iframe.有没有办法将StreamReader与URI格式一起使用?如果没有,使用C#读取HTML的txt文件还有哪些其他选项?如果这不是最佳选择,那么有人可以建议一种更好的方法吗?

I have a file at a URI that I would like to read using StreamReader. Obviously, this causes a problem since File.OpenText does not support URI paths. The file is a txt file with a bunch of html in it. I have multiple web pages that use this same piece of html, so I have put it in a txt file, and am reading it into the page when the page loads (I can get it to work when I put the file on the file system, but need to put it in a document repository online so that a business user can get to it). I am trying to avoid using an iframe. Is there a way to use StreamReader with URI formats? If not, what other options are there using C# to read in the txt file of html? If this is not optimal, can someone suggest a better approach?

推荐答案

如果您位于代理之后,请不要忘记设置凭据:

If you are behind a proxy don't forget to set your credentials:

  WebRequest request=WebRequest.Create(url);
  request.Timeout=30*60*1000;
  request.UseDefaultCredentials=true;
  request.Proxy.Credentials=request.Credentials;
  WebResponse response=(WebResponse)request.GetResponse();
  using (Stream s=response.GetResponseStream())
    ...

这篇关于如何使用StreamReader从URI读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆