gpt4 book ai didi

c# - 如何在 c# 中检查 HttpWebRequest 中的二进制与文本?

转载 作者:行者123 更新时间:2023-12-04 05:01:25 25 4
gpt4 key购买 nike

有没有办法确定是否来自 HttpWebRequest 的响应?在 C# 中包含二进制数据与文本?或者我应该使用另一个类或函数来做到这一点吗?

这是一些示例代码。在阅读 StreamReader 之前,我想知道如果内容不是文字。

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.someurl.com");
request.Method = WebRequestMethods.Http.Get;
using (WebResponse response = request.GetResponse())
{
// check somewhere in here if the response is binary data and ignore it
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string responseDetails = reader.ReadToEnd().Trim();
}
}

最佳答案

通常,网站会在 Content-Type header 中告诉您它们返回的数据类型。您可以通过获取 ContentType 来确定这一点。来自响应的属性。

但众所周知,网站会撒谎。或者什么都不说。我两个都见过。如果没有 Content-Type header 或者您不想信任它,那么您可以通过阅读来判断那里的数据类型的唯一方法。

但是,如果您不信任该站点,为什么要从中读取数据?

关于c# - 如何在 c# 中检查 HttpWebRequest 中的二进制与文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16113657/

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