我正在制作一个C#控制台应用程序来加密和上传文件到Backblaze B2备份。我正在使用HttpWebRequest使用他们的API上传文件,但是,当我上传任何大于4MB的内容时,我会从"Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host."
获得IOException stream.Write
。我必须根据B2 API使用HTTP POST请求,那么有没有办法超过.Net的4MB限制?
我的相关代码是
internal void UploadFile(FileInfo f, string sha1) {
HttpWebRequest webRequest = WebRequest.CreateHttp(this.uploadUrl);
webRequest.Method = "POST";
webRequest.Headers.Add("Authorization", this.uploadAuthorizationToken);
webRequest.Headers.Add("X-Bz-File-Name", f.Name);
webRequest.Headers.Add("X-Bz-Content-Sha1", sha1);
webRequest.ContentType = "application/octet-stream";
webRequest.ContentLength = f.Length;
byte[] bytes = File.ReadAllBytes(f.FullName);
using (var stream = webRequest.GetRequestStream()) {
stream.Write(bytes, 0, bytes.Length); //IOException occurs here
stream.Close();
}
WebResponse response = (HttpWebResponse)webRequest.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
response.Close();
Console.WriteLine("b2_upload_file:\n" + responseString);
}
b2_upload_file https://www.backblaze.com/b2/docs/b2_upload_file.html
的文档