Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#HTTP PUT请求代码存在问题_C#_Http_Web - Fatal编程技术网

C#HTTP PUT请求代码存在问题

C#HTTP PUT请求代码存在问题,c#,http,web,C#,Http,Web,我试图通过AmazonS3已经为我生成的PUT请求URL向S3发送一个文件 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 i

我试图通过AmazonS3已经为我生成的PUT请求URL向S3发送一个文件

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);
我的代码适用于小文件,但在发送几分钟后,在大文件(>100MB)上会出错

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);
错误为:请求已中止:请求已取消。在System.Net.ConnectStream.InternalWrite(布尔异步、字节[]缓冲区、Int32偏移量、Int32大小、异步回调、对象状态) 在System.Net.ConnectStream.Write(字节[]缓冲区,Int32偏移量,Int32大小)

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);
有人能告诉我我的代码有什么问题,阻止它发送大文件吗?这不是因为Amazon PUT请求URL过期,因为我将其设置为30分钟,而问题发生在发送几分钟后

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);
代码最终在这一行代码中出现异常:
dataStream.Write(byteArray,0,byteArray.Length)

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);
再一次,它对我发送到S3的较小文件非常有用。只是不是大文件

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 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);
request.ContentLength = byteArray.LongLength;
而不是:

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);
request.ContentLength = byteArray.Length;

再想一想,100 MB=100*1024*1024<2^32,所以可能不会有问题

使用或比较当它工作时(第三方工具)和不工作时(您的代码)。。。一旦你知道了它们之间的区别,你就可以相应地修改你的代码了……

我会试着把它写成块,然后拆分字节数组。它可能被一大块东西噎住了

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 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);
        const int chunkSize = 500;
        for (int i = 0; i < byteArray.Length; i += chunkSize)
        {
            int count = i + chunkSize > byteArray.Length ? byteArray.Length - i : chunkSize;
            dataStream.Write(byteArray, i, count);
        }
const int chunkSize=500;
for(int i=0;ibyteArray.Length?byteArray.Length-i:chunkSize;
写入(字节数组,i,计数);
}

可能需要再次检查以确保它编写了所有内容,我只对它进行了非常基本的测试。

您应该将的属性设置为更高的值。这会导致请求在完成之前超时。

这可能很明显,但您确定Amazon的S3服务允许HTTP请求超过100MB吗?我认为这不是问题所在。完全100mb的数据永远不会被发送(一两分钟后就会停止)。我可以使用任何第三方S3软件程序发送文件,他们通过PUT请求发送文件,并且工作正常。我认为我的代码除了S3的问题之外还有其他问题。在上面的代码中,您的超时时间只有6分钟,而不是30分钟。我在创建S3 PUT URL时使用的超时时间是30分钟。URL链接在30分钟后过期。我的代码中的超时是连接超时,设置为6分钟。超时不是问题,因为大文件传输甚至从未达到6分钟的时间。如果您将数据流分块写入呢?在传输了一定数量的字节后,它是否总是失败?这没有任何效果。无论如何谢谢你。@fraXis结束了。将块大小更改为您认为合适的大小,例如8KB或其他大小,500字节只是一个示例。非常感谢。这就是问题所在。我假设这是一个连接超时,而不是超过时停止传输的实际超时。
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);