gpt4 book ai didi

c# - 下载前 1000 个字节

转载 作者:太空狗 更新时间:2023-10-29 20:02:02 25 4
gpt4 key购买 nike

我需要使用 C# 从 Internet 下载一个文本文件。文件大小可能非常大,我需要的信息总是在前 1000 个字节以内。这可能吗?

最佳答案

盗自 here .

string GetWebPageContent(string url)
{
string result = string.Empty;
HttpWebRequest request;
const int bytesToGet = 1000;
request = WebRequest.Create(url) as HttpWebRequest;

//get first 1000 bytes
request.AddRange(0, bytesToGet - 1);

// the following code is alternative, you may implement the function after your needs
using (WebResponse response = request.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
byte[] buffer = new byte[1024];
int read = stream.Read(buffer, 0, 1000);
Array.Resize(ref buffer, read);
return Encoding.ASCII.GetString(buffer);
}

}
}

(根据评论中的要求编辑...;))

关于c# - 下载前 1000 个字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3853495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com