Nei*_*rno 0 c# httpwebrequest character-encoding httpwebresponse
我无法使用WebClient,在任何人建议之前,因为它使我的合法应用程序看起来像迈克菲病毒.所以请不要这么做.
我有一个存储在我的服务器上的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());
Run Code Online (Sandbox Code Playgroud)
(服务器上的binary.txt文件的内容是ASCII,因此我也将Encoding字符串转换为ASCII).
这是我编码该文本文件的方式(在该服务器上)
我的文件基本上是这样的:
byte[] bytes = File.ReadAllBytes("binary.txt");
String encBytes = Encoding.ASCII.GetString(bytes);
File.WriteAllText(file("binary.txt"), encBytes);
Run Code Online (Sandbox Code Playgroud)
我联系了AV公司关于WebDownloader被视为C#中的一些恶意导入,但他们没有回复我,所以我被迫使用HttpWebRequest.
如果您的唯一目标是获取该二进制文件并将其写入磁盘,则只需CopyTo使用Stream对象上存在的方法将该流复制到文件即可.
您的文件看起来像一个破损的zip文件btw,给定第一个字符PK,并在zip规范中使用.
来自维基百科:
这是一个ASCII字符串,它写着"PK",这是发明家Phil Katz的缩写.因此,当在文本编辑器中查看.ZIP文件时,文件的前两个字节通常是"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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4748 次 |
| 最近记录: |