gpt4 book ai didi

http - 通过 TcpClient 从 HTTP 服务器下载文件

转载 作者:可可西里 更新时间:2023-11-01 16:39:33 25 4
gpt4 key购买 nike

我正在学习 HTTP,并决定使用 TcpClient 从 Internet 下载文件。

private void requireBtn_Click(object sender, EventArgs e) {
var client = new TcpClient(addressBox.Text, 80);
var networkStream = client.GetStream();

var sWriter = new StreamWriter(networkStream, Encoding.Default);
foreach (string querLine in queryBox.Lines) {
sWriter.WriteLine(querLine);
}
sWriter.WriteLine();
sWriter.Flush();

string responceText = "";

var sReader = new StreamReader(networkStream);
string respLine;
while ((respLine = sReader.ReadLine()).Length > 0) {
responceText += respLine + "\r\n";
}

responceBox.Text = responceText;

Regex reContentLength = new Regex(@"(?<=Content-Length:\s)\d+", RegexOptions.IgnoreCase);
Int32 contentLength = Int32.Parse(reContentLength.Match(responceText).Value);
this.Text = contentLength.ToString();

if (networkStream.CanRead) {
var fileStream = new FileStream(@"C:\img.png", FileMode.Create);

byte[] buffer = new byte[1024];

int numberOfBytesRead = 0;
do {
numberOfBytesRead = networkStream.Read(buffer, 0, buffer.Length);
fileStream.Write(buffer, 0, numberOfBytesRead);
}
while (networkStream.DataAvailable);

fileStream.Flush();
fileStream.Close();

MessageBox.Show("Done!");
}
else
MessageBox.Show("Fail!");

client.Close();
}

但是下载的文件已损坏。我不明白为什么... Source file

最佳答案

private void requireBtn_Click(object sender, EventArgs e) {
string host = "atomAltera.SkyHost.ge";
string query = "GET /mems.txt HTTP/1.1\r\n" +
"Host: atomAltera.SkyHost.ge\r\n" +
"User-Agent: NuclightWeb\r\n" +
"Connection: close\r\n"+
"\r\n";

var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
client.Connect(host, 80);

var networkStream = new NetworkStream(client);

var bytes = Encoding.Default.GetBytes(query);
networkStream.Write(bytes, 0, bytes.Length);


var bReader = new BinaryReader(networkStream, Encoding.Default);

string responce = "";
string line;
char c;

do {
line = "";
c = '\u0000';
while (true) {
c = bReader.ReadChar();
if (c == '\r')
break;
line += c;
}
c = bReader.ReadChar();
responce += line + "\r\n";
} while (line.Length > 0);

responceBox.Text = responce;

Regex reContentLength = new Regex(@"(?<=Content-Length:\s)\d+", RegexOptions.IgnoreCase);
Int32 contentLength = Int32.Parse(reContentLength.Match(responce).Value);

this.Text = contentLength.ToString();

var fileStream = new FileStream(@"C:\m.txt", FileMode.Create);

byte[] buffer = new byte[4 * 1024];
int n = 0;
int read = 0;

while (n < contentLength) {
if (networkStream.DataAvailable) {
read = networkStream.Read(buffer, 0, buffer.Length);
n += read;
fileStream.Write(buffer, 0, read);
}
}

fileStream.Flush();
fileStream.Close();


client.Close();
}

有效!可能是 StreamReader 读取了多余的字节?

关于http - 通过 TcpClient 从 HTTP 服务器下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4768443/

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