- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个HTTP协议(protocol)的下载方法。但它似乎无法正常工作,出了点问题。我用一些 url 源测试了它,除了最后一个之外,它是正确的。 ContentLength
属性对于 url 是错误的。它在运行时显示为 210 kb,但实际上是 8 MB。我将通过分享我的代码来展示它。如何解决?
代码:
void TestMethod(string fileUrl)
{
HttpWebRequest req = WebRequest.Create(fileUrl) as HttpWebRequest;
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
long contentSize = resp.ContentLength;
MessageBox.Show(contentSize.ToString());
}
private void TestButton_Click(object sender, EventArgs e)
{
string url1 = "http://www.calprog.com/Sounds/NealMorseDowney_audiosample.mp3";
string url2 = "http://www.stephaniequinn.com/Music/Canon.mp3";
TestMethod(url1); //This file size must be 8 MB, but it shows up as 210 kb. This is the problem
TestMethod(url2); //This file size is correct here, about 2.1 MB
}
最佳答案
我认为您不允许以这种方式访问此 url(使用 HttpWebRequest)。
如果您尝试获取响应文本:
HttpWebRequest req = WebRequest.Create(fileUrl) as HttpWebRequest;
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
using (var streamreader = new StreamReader(resp.GetResponseStream()))
{
var r = streamreader.ReadToEnd();
long contentSize = r.Length;
Console.WriteLine(contentSize.ToString());
}
你会得到这样的回应:
<html><head><title>Request Rejected</title></head><body>The requested URL was rejected. If you think this is an error, please contact the webmaster. <br><br>Your support ID is: 2719994757208255263</body></html>
您必须设置 UserAgent 才能获得完整的响应。像这样:
req.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0";
通过设置这个值,服务器会认为您的程序是 Firefox 浏览器。
所以这几行代码应该可以解决问题:
void TestMethod(string fileUrl)
{
HttpWebRequest req = WebRequest.Create(fileUrl) as HttpWebRequest;
req.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0";
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
long contentSize = resp.ContentLength;
Console.WriteLine(contentSize.ToString());
}
祝你有美好的一天!
关于c# - HttpWebResponse 对于 ContentLength 是错误的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39560582/
我有一个面向“.NET for Windows Store apps”和“Windows Phone 7.5 或更高版本”的可移植类库。我发出了 HTTP POST 请求,截至上周,负责后端的管理员决
我正在尝试通过http请求从es获取数据,但是在传递rquest正文之后,出现此错误: http:紧急服务127.0.0.1:13995:获取 “http://127.0.0.1:9200/demo/
我在我的应用程序中使用了一项服务,我曾经改造它以在后台下载一些“apk”文件。我想用收到的文件大小检查下载的文件大小,以确保我完全获得“apk”。但是当我使用 response.body().cont
我有一个获取 URL 并下载 .jpeg 文件的 Windows 应用程序。对于某些 url,ContentLength 属性为 -1,因此会抛出异常。 这是我的代码: var url = new U
我有一个HTTP协议(protocol)的下载方法。但它似乎无法正常工作,出了点问题。我用一些 url 源测试了它,除了最后一个之外,它是正确的。 ContentLength 属性对于 url 是错误
很奇怪... 为了将一些 JSON 数据发布到我的服务器,我将 contentLength 定义为 JSON 编码数据的长度,但随后我收到一个异常,提示“内容大小超过指定的 contentLength
我有这个函数来执行http请求 func do(r *http.Request) (response *http.Response, e error) { r.Header.Set(Sessi
我正在与一家公司合作,希望他们能够将文件放入我的 Azure Blobstorage 中。我的想法是,我生成授权 header ,并使用他们的 REST API 将这个 header 和其他必需的 h
这是一个愚蠢的问题,但我正在测试一个工具,需要创建一个不寻常的情况。我想发出一个 POST 请求,将几兆字节的数据发送到 Web 服务器,但我不希望设置 Content-Length。 (设置为0或-
请告诉我 InputFile.PostedFile.ContentLength 测量的单位。我需要确保文件小于 500k。 谢谢。 最佳答案 测量单位 = 字节。 1 Kilobyte (kB) =
我需要一种方法来确定 Internet 上文件的大小。这是我的方法: private int getSize(String url){ try { HttpURLCon
我们这里有一个特殊的问题。我们在客户端有一个代码,可以使用 HTTPWebResponse/HttpWebRequest 从 Web 服务器下载一些文件我们正在使用 HttpWebResponse.C
在 Delphi 中使用此代码获取网页大小:(我的意思是页面源大小) uses IdHTTP function URLsize(const URL : string) : integer; var
对 非常陌生语言 , 不到 10 天。我有一个 http 服务器,我需要 http 服务磁盘内的文件。这里默认使用“net/http”http.ServeFile(w, r, file)。 我的问题是
我正在将位图图像上传到 Azure Blob 存储。一切都工作正常,但后来我创建了一个新项目来使用相同的代码,但现在我始终收到此错误“要写入的字节数大于指定的ContentLength” 这是我的代码
在自定义 Tomcat Valve 中,有什么方法可以在内容被压缩后获取 contentLength? response.getContentCountLong() 返回压缩前的大小。 最佳答案 使用
我对 jQuery AJAX 和 ASSX 行为有奇怪的问题。这是我的代码: function Upload() { var data = new FormData(); jQue
将 HttpWebRequest.AutomaticDecompression 设置为 gzip 后,响应的 ContentLength 属性返回 -1。 (在你说之前,是的,有响应内容!)这是一个大
如果出现连接/代理错误,我正在尝试重试请求。由于某些原因,我不断收到此错误,无论尝试重试请求,该错误似乎都无法恢复: Post https://m.somewebsite.co.uk/api/
我正在寻找以下问题的答案: 对于任何给定的请求,这三个属性是否总是返回相同的值? 它们中的任何一个有副作用吗? 在 IIS 收到整个请求之前,它们中的任何一个都会阻塞吗? 它们中的任何一个会导致上传的
我是一名优秀的程序员,十分优秀!