C#HTTP PUT请求代码问题 [英] Problem with C# HTTP PUT request code
问题描述
我正在尝试通过Amazon S3已经为我生成的PUT请求URL将文件发送到S3。
I am trying to send a file to S3 via a PUT request URL that Amazon S3 has already generated for me.
我的代码适用于小文件,但是发送几分钟后,它在大文件(> 100 mb)上出错。
My code works fine for small files, but it errors out on large files (>100 mb) after a few minutes of sending.
出现错误: 请求已中止:请求取消了。 at System.Net.ConnectStream.InternalWrite(Boolean async,Byte [] buffer,Int32 offset,Int32 size,AsyncCallback callback,Object state)
at System.Net.ConnectStream.Write(Byte [] buffer,Int32 offset, Int32大小)
There error is: The request was aborted: The request was canceled. at System.Net.ConnectStream.InternalWrite(Boolean async, Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state) at System.Net.ConnectStream.Write(Byte[] buffer, Int32 offset, Int32 size)
有人可以告诉我我的代码有什么问题阻止它发送大文件吗?这不是因为Amazon PUT请求URL过期,因为我将其设置为30分钟,并且在发送几分钟后就会出现问题。
Can someone please tell me what is wrong with my code that is stopping it from sending large files? It is not due to the Amazon PUT request URL expiring because I have that set to 30 minutes and the problem occurs after just a few minutes of sending.
代码最终异常在这行代码中: dataStream.Write(byteArray,0,byteArray.Length);
The code eventually exceptions out on this line of code: dataStream.Write(byteArray, 0, byteArray.Length);
再一次,它适用于我发送给S3的较小文件。只是不是大文件。
Once again, it works great for smaller files that I am sending to S3. Just not large files.
WebRequest request = WebRequest.Create(PUT_URL_FINAL[0]);
//PUT_URL_FINAL IS THE PRE-SIGNED AMAZON S3 URL THAT I AM SENDING THE FILE TO
request.Timeout = 360000; //6 minutes
request.Method = "PUT";
//result3 is the filename that I am sending
request.ContentType =
MimeType(GlobalClass.AppDir + Path.DirectorySeparatorChar + "unzip" +
Path.DirectorySeparatorChar +
System.Web.HttpUtility.UrlEncode(result3));
byte[] byteArray =
File.ReadAllBytes(
GlobalClass.AppDir + Path.DirectorySeparatorChar + "unzip" +
Path.DirectorySeparatorChar +
System.Web.HttpUtility.UrlEncode(result3));
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
// this is the line of code that it eventually quits on.
// Works fine for small files, not for large ones
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
//This will return "OK" if successful.
WebResponse response = request.GetResponse();
Console.WriteLine("++ HttpWebResponse: " +
((HttpWebResponse)response).StatusDescription);
推荐答案
你应该设置 超时
属性 WebRequest
更高的价值。它会导致请求在完成之前超时。
You should set the Timeout
property of the WebRequest
to a higher value. It causes the request to time out before it is completed.
这篇关于C#HTTP PUT请求代码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!