如何从URL导入文本?

时间:2018-05-21 13:03:35

标签: c# string import

如何将text from URL导入以下c#代码?

        string text = "Import Text Here";

        string newText= text.ToLower();

        Console.WriteLine(newText);

1 个答案:

答案 0 :(得分:1)

如果&#34; 从网址<#34>导入文字;实际上意味着下载

  using System.Net;

  ...

  string address = @"http://www.gutenberg.org/files/10571/10571.txt";
  string newText = null;

  HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
  // in case you work via some kind of proxy
  request.Credentials = CredentialCache.DefaultCredentials; 

  //TODO: simplest; you way want to use Async versions
  using (var response = request.GetResponse()) {
    using (var reader = new StreamReader(response.GetResponseStream())) {
      newText = reader.ReadToEnd().ToLower();
    }
  }

现在newText包含以下文字:

 the project gutenberg ebook of the old man of the sea, by w.w. jacobs
 ... 
 title: the old man of the sea
   ship's company, part 11.
 ... 
 an alternative method of locating ebooks:
 http://www.gutenberg.net/gutindex.all