gpt4 book ai didi

c# - 使用 c# 在 HttpWebRequest 中添加预告片

转载 作者:太空宇宙 更新时间:2023-11-03 23:43:10 25 4
gpt4 key购买 nike

我试图在 HttpWebRequest header 中添加预告片,但它没有在文件数据末尾附加该预告片。

wreq.ContentType = "application/octet-stream";
wreq.AllowWriteStreamBuffering = false;
wreq.SendChunked = true;
//wreq.Headers.Add(HttpRequestHeader.Te, "trailers");
wreq.Headers.Add(HttpRequestHeader.Trailer, "Test");
wreq.Headers["Test"] = "the-value";

using (Stream POSTstream = wreq.GetRequestStream())
{
//dataByte is file-data in byte[]
POSTstream.Write(dataByte, 0, dataByte.Length);
POSTstream.Flush();
//hashValue is trailer in byte[]
POSTstream.Write(hashValue, 0, hashValue.Length);
POSTstream.Flush();
POSTstream.Close();
}

它应该在空白 block 之后附加此预告片“Test”@EOF,但它不会附加它。当我尝试以编程方式添加预告片时,它会将其视为文件数据而不是预告片。

预期请求:

POST <URL> HTTP/1.1
Content-Type: application/octet-stream
Trailer: Test
Transfer-Encoding: chunked

5d
File-data

0
Test: the-value

实际请求:

POST <URL> HTTP/1.1
Content-Type: application/octet-stream
Trailer: Test
Transfer-Encoding: chunked

5d
File-data

5A
Test: the-value
0

为什么这个测试预告片没有找到空白 block 。该预告片将在服务器上用于识别文件结尾。请帮忙。

最佳答案

经过一个多星期的研究,我了解到 Dotnet 不允许在 httprequest 中添加预告片。为了实现上述预期请求,我使用了 Node.js。为此,

首先,我创建了 app.js 文件,其中包含用于创建带有预告片的请求并从服务器获取响应的代码:

var http = require('http');
var fs = require('fs');
var options = {hostname: 'ABC',port: 6688,path: 'XYZ/101/-3/test.file',method: 'POST',
headers: {'Content-type' : 'application/octet-stream','Trailer' : 'Test','Transfer-Encoding': 'chunked'}};
var fileContent = fs.readFileSync('C:\\test.file');
var req = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {fs.writeFile('C:\\response.xml', chunk);});});
req.on('error', function(e) {fs.writeFile('C:\\response.xml','Error: ' + e.message);});
var len = fileContent.length;
var bufSize = 4096;
for (var i = 0 ; i < len ; ) {
if (i+bufSize <len){req.write(fileContent.slice(i, i+bufSize));}
else{req.write(fileContent.slice(i,len));req.addTrailers({'Test': 'TESTTEST'});req.end();}i = i +bufSize;
}

并从 dotnet 作为应用程序运行此 app.js 文件并获取响应:

//build app.js file content
var template = GetAppJSString();
var appjsfile = "C:\\app.js";
using (var sw = new StreamWriter(appjsfile))
{
sw.Write(template);
sw.Close();
}

var psi = new ProcessStartInfo
{
CreateNoWindow = true,
FileName = "C:\\Node.exe",
Arguments = "\"" + appjsfile + "\"",
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden
};

try
{
_nodeProcess.StartInfo = psi;
_nodeProcess.Start();
_nodeProcess.WaitForExit();

//read and process response
var responseText = string.Empty;
using (var sr = new StreamReader("C:\\response.xml"))
{
responseText = sr.ReadToEnd();
sr.Close();
}
File.Delete("C:\\response.xml");
// process response

}
catch (Exception ex) { }

希望这能帮助其他人并节省他们的时间。

关于c# - 使用 c# 在 HttpWebRequest 中添加预告片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18980153/

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