Get text from txt file on the internet

半腔热情 提交于 2019-12-30 07:24:07

问题


I have a uwp and i need to get text from a txt file saved on the internet to string I have a problem with download the file and get the text to string

Here's my code:

            var webRequest = WebRequest.Create(@"http://yourUrl");
        using (var response = webRequest.GetResponse())
        using (var content = response.GetResponseStream())
        using (var reader = new StreamReader(content))
        {
            var strContent = reader.ReadToEnd();
        }

can anyone help me?


回答1:


var url = "http://example.com/abc.txt";
var textFromFile = (new WebClient()).DownloadString(url);



回答2:


I find an answer to my question

I've to download the txt file to my local storage and after that i read them from my local

Here's the code that i used to download the file to my local folder

            var uriBing = new Uri(@"https://your/abc.txt");
        StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
        StorageFile sampleFile = await storageFolder.CreateFileAsync("status.txt", CreationCollisionOption.ReplaceExisting);
        var cli = new HttpClient();
        Byte[] bytes = await cli.GetByteArrayAsync(uriBing);
        IBuffer buffer = bytes.AsBuffer(); await Windows.Storage.FileIO.WriteBufferAsync(sampleFile, buffer);

Thanks for helping hope this will help anyone look for something like this



来源:https://stackoverflow.com/questions/46062032/get-text-from-txt-file-on-the-internet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!