WebRequest - .timeout 问题

发布于 2024-12-01 05:01:01 字数 1658 浏览 0 评论 0原文

有关更多背景信息,请参阅:C# HTTP PUT 请求代码的问题

我正在使用 C# WebRequest 方法将文件上传到 S3。我已经有了 S3 为我提供的用于上传的预签名 URL。我最初遇到一个问题,如果我上传一个大文件,传输会在开始后几分钟停止。我的另一篇文章的评论说是 .timeout 造成的。果然是这样。

我的问题是:我应该将 .timeout 设置为什么,或者如何根据我要传输的文件的大小来计算设置它的功能?

当我只想向 S3 发送一个小文件时,我不想启动一个 60 分钟超时的 WebRequest,但是我也可以将一个千兆字节的文件上传到 S3,如果在文件完成之前超时,传输将被中断。

另外我如何捕获连接超时? .timeout 也会用于此目的吗?如果我只想尝试连接 30 秒以查看 S3 是否响应我的 Web PUT 请求,如果没有响应,则超时,该怎么办?

我的代码是:

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 -- If the transfer is over 6 minutes, it stops.

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();

dataStream.Write(byteArray, 0, byteArray.Length); 

dataStream.Close();

//This will return "OK" if successful.
WebResponse response = request.GetResponse();
Console.WriteLine("++ HttpWebResponse: " +
                  ((HttpWebResponse)response).StatusDescription);

For a little more background, please see: Problem with C# HTTP PUT request code

I am using the C# WebRequest method to upload a file to S3. I already have a pre-signed URL that S3 is giving me for the upload. I originally had a problem where if I was uploading a large file, the transfer would stop a few minutes after starting. A comment on my other post said it was the .timeout that was causing it. Sure enough it was.

My question is this: What should I set the .timeout to or how can I calculate what to set it do based on the size of the file that I am going to transfer?

I don't want to start a WebRequest with a 60 minute timeout when I am only going to be sending a small file to S3, but then again I could also be uploading a gigabyte file to S3 also and if the timeout goes off before the file is done, the transfer will be interrupted.

Also how would I trap for a connection timeout? Would the .timeout be used for that also? What if I only want to try a connection for 30 seconds to see if S3 responds to my web PUT request and if doesn't then have it timeout.

My code is:

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 -- If the transfer is over 6 minutes, it stops.

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();

dataStream.Write(byteArray, 0, byteArray.Length); 

dataStream.Close();

//This will return "OK" if successful.
WebResponse response = request.GetResponse();
Console.WriteLine("++ HttpWebResponse: " +
                  ((HttpWebResponse)response).StatusDescription);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小ぇ时光︴ 2024-12-08 05:01:01

您可以尝试根据文件大小计算超时 - 但这样做意味着您必须知道或估计一些传输速度...

另一种选择是在通常情况下选择“可接受”的超时,然后在获取时异常以“标准超时”的两倍或十倍重试...

you could try to calculate the timeout depending on your file size - but to do so means you must know or estimate some transfer speed...

Another option would be to choose your timeout in an "acceptable" for the usual cases and then when getting the exception retrying with double or ten times the "standard timeout"...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文