HttpWebRequest 没有正确下载文件
HttpWebRequest not downloading file correctly
在有人建议之前我不能使用 WebClient
,因为它使我的合法应用程序看起来像 McAfee 的病毒。所以请不要这样建议。
我的服务器上存储了一个 binary.txt 文件。它大约是 1,240kb。然而,HttpWebRequest 的下载量从 1,300kb 到 1,700kb 不等。
HttpWebRequest httpRequest = (HttpWebRequest)
WebRequest.Create("http://deviantsmc.com/binary.txt");
httpRequest.Method = WebRequestMethods.Http.Get;
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
Stream httpResponseStream = httpResponse.GetResponseStream();
byte[] buffer = new byte[1240];
int bytesRead = 0;
StringBuilder sb = new StringBuilder();
//FileStream fileStream = File.Create(@"tcontent.txt");
while ((bytesRead = httpResponseStream.Read(buffer, 0, 1240)) != 0)
{
sb.Append(Encoding.ASCII.GetString(buffer));
//fileStream.Write(buffer, 0, bytesRead);
}
File.WriteAllText(@"tcontent1.txt", sb.ToString());
(服务器上binary.txt文件的内容是ASCII,因此我也得到了编码字符串为ASCII)。
这就是我对该文本文件(在该服务器上)进行编码的方式
我的文件基本上是这样的:
byte[] bytes = File.ReadAllBytes("binary.txt");
String encBytes = Encoding.ASCII.GetString(bytes);
File.WriteAllText(file("binary.txt"), encBytes);
我联系了 AV 公司关于 WebDownloader
在 C# 中被视为恶意导入,但他们没有回复我,所以我被迫使用 HttpWebRequest
。
如果您的唯一目标是获取该二进制文件并将其写入磁盘,您只需将流复制到包含 CopyTo
method that exists on a Stream
对象的文件即可。
你的文件看起来像一个损坏的 zip 文件,顺便说一下,鉴于第一个字符是 PK
,并且在 zip 规范中使用。
来自 wikipedia:
Viewed as an ASCII string this reads "PK", the initials of the inventor Phil Katz. Thus, when a .ZIP file is viewed in a text editor the first two bytes of the file are usually "PK".
我使用 7-zip 打开您的文件,因为 Windows 默认不接受它作为有效文件。它包含一个 manifest.mf 文件,但内容本身似乎丢失了。该文件本身的大小为 1.269.519 字节。
HttpWebRequest httpRequest = (HttpWebRequest)
WebRequest.Create("http://deviantsmc.com/binary.txt");
httpRequest.Method = WebRequestMethods.Http.Get;
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
Stream httpResponseStream = httpResponse.GetResponseStream();
// create and open a FileStream, using calls dispose when done
using(var fs= File.Create(@"c:\temp\bin.7z"))
{
// Copy all bytes from the responsestream to the filestream
httpResponseStream.CopyTo(fs);
}
我觉得你应该把整个二进制数据写入本地文件,而不是分段编码,你可以试试这个:
HttpWebRequest httpRequest = (HttpWebRequest)
WebRequest.Create("http://deviantsmc.com/binary.txt");
httpRequest.Method = WebRequestMethods.Http.Get;
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
Stream httpResponseStream = httpResponse.GetResponseStream();
using (BinaryReader responseReader = new BinaryReader(httpResponseStream.GetResponseStream()))
{
byte[] bytes = responseReader.ReadBytes((int)response.ContentLength);
using (BinaryWriter sw = new BinaryWriter(File.OpenWrite("tcontent1.txt")))
{
sw.Write(bytes);
sw.Flush();
sw.Close();
}
}
希望对您有所帮助
在有人建议之前我不能使用 WebClient
,因为它使我的合法应用程序看起来像 McAfee 的病毒。所以请不要这样建议。
我的服务器上存储了一个 binary.txt 文件。它大约是 1,240kb。然而,HttpWebRequest 的下载量从 1,300kb 到 1,700kb 不等。
HttpWebRequest httpRequest = (HttpWebRequest)
WebRequest.Create("http://deviantsmc.com/binary.txt");
httpRequest.Method = WebRequestMethods.Http.Get;
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
Stream httpResponseStream = httpResponse.GetResponseStream();
byte[] buffer = new byte[1240];
int bytesRead = 0;
StringBuilder sb = new StringBuilder();
//FileStream fileStream = File.Create(@"tcontent.txt");
while ((bytesRead = httpResponseStream.Read(buffer, 0, 1240)) != 0)
{
sb.Append(Encoding.ASCII.GetString(buffer));
//fileStream.Write(buffer, 0, bytesRead);
}
File.WriteAllText(@"tcontent1.txt", sb.ToString());
(服务器上binary.txt文件的内容是ASCII,因此我也得到了编码字符串为ASCII)。
这就是我对该文本文件(在该服务器上)进行编码的方式
我的文件基本上是这样的:
byte[] bytes = File.ReadAllBytes("binary.txt");
String encBytes = Encoding.ASCII.GetString(bytes);
File.WriteAllText(file("binary.txt"), encBytes);
我联系了 AV 公司关于 WebDownloader
在 C# 中被视为恶意导入,但他们没有回复我,所以我被迫使用 HttpWebRequest
。
如果您的唯一目标是获取该二进制文件并将其写入磁盘,您只需将流复制到包含 CopyTo
method that exists on a Stream
对象的文件即可。
你的文件看起来像一个损坏的 zip 文件,顺便说一下,鉴于第一个字符是 PK
,并且在 zip 规范中使用。
来自 wikipedia:
Viewed as an ASCII string this reads "PK", the initials of the inventor Phil Katz. Thus, when a .ZIP file is viewed in a text editor the first two bytes of the file are usually "PK".
我使用 7-zip 打开您的文件,因为 Windows 默认不接受它作为有效文件。它包含一个 manifest.mf 文件,但内容本身似乎丢失了。该文件本身的大小为 1.269.519 字节。
HttpWebRequest httpRequest = (HttpWebRequest)
WebRequest.Create("http://deviantsmc.com/binary.txt");
httpRequest.Method = WebRequestMethods.Http.Get;
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
Stream httpResponseStream = httpResponse.GetResponseStream();
// create and open a FileStream, using calls dispose when done
using(var fs= File.Create(@"c:\temp\bin.7z"))
{
// Copy all bytes from the responsestream to the filestream
httpResponseStream.CopyTo(fs);
}
我觉得你应该把整个二进制数据写入本地文件,而不是分段编码,你可以试试这个:
HttpWebRequest httpRequest = (HttpWebRequest)
WebRequest.Create("http://deviantsmc.com/binary.txt");
httpRequest.Method = WebRequestMethods.Http.Get;
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
Stream httpResponseStream = httpResponse.GetResponseStream();
using (BinaryReader responseReader = new BinaryReader(httpResponseStream.GetResponseStream()))
{
byte[] bytes = responseReader.ReadBytes((int)response.ContentLength);
using (BinaryWriter sw = new BinaryWriter(File.OpenWrite("tcontent1.txt")))
{
sw.Write(bytes);
sw.Flush();
sw.Close();
}
}
希望对您有所帮助