gpt4 book ai didi

使用 HttpWebRequest 时 Silverlight 5 插件崩溃或请求中止

转载 作者:行者123 更新时间:2023-12-03 04:39:32 26 4
gpt4 key购买 nike

我尝试在 azure blob 存储上上传图片,但完成起来遇到很大困难。

我使用下面的代码来上传:

public static void Upload(this Stream payLoad, string uri)
{
var webRequest = (HttpWebRequest)WebRequestCreator.ClientHttp.Create(new Uri(uri));
webRequest.Method = "PUT";
webRequest.ContentLength = payLoad.Length;

var state = new HttpWebRequestAsyncState<Stream>() { HttpWebRequest = webRequest, PayLoad = payLoad };

Task.Factory.FromAsync<Stream>(
webRequest.BeginGetRequestStream,
webRequest.EndGetRequestStream, state)
.ContinueWith(t1 =>
{
var asyncState1 = (HttpWebRequestAsyncState<Stream>)t1.AsyncState;

using (var requestStream = t1.Result)
{
int bytesRead = 0;
byte[] buffer = new Byte[4096];
while ((bytesRead = asyncState1.PayLoad.Read(buffer, 0, buffer.Length)) != 0)
{
requestStream.Write(buffer, 0, bytesRead);
requestStream.Flush();
}
}

Task.Factory.FromAsync<WebResponse>(
asyncState1.HttpWebRequest.BeginGetResponse,
asyncState1.HttpWebRequest.EndGetResponse,
asyncState1)
.ContinueWith(t2 =>
{
var asyncState2 = (HttpWebRequestAsyncState<Stream>)t2.AsyncState;
if (asyncState2.PayLoad != null)
{
asyncState2.PayLoad.Close();
}

using (var responce = t2.Result)
{
if (responce != null) responce.Close();
}
});
});
}

当我在我的开发机器(带有 Windows 2008 R2 64 位的 vmware)上运行此代码时,当我在循环中调用该代码时,我会遇到插件崩溃,并且没有我可以在我的级别使用的信息。

当我在我的个人 Windows 7 Ultimate 64 位上运行此代码时,有些文件被上传,有些则没有,并且在 fiddler 中显示状态: session 被客户端、Fiddler 或服务器中止。

调用此函数的代码是:

private void FileUploadButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog() { Multiselect = true, Filter = "Image files (*.jpg)|*.jpg" };

if (ofd.ShowDialog() != true) return;

foreach (var file in ofd.Files)
{
string uri = "http://xxxx.blob.core.windows.net/test/" + file.Name + "?sr=c&si=dev&sig=%2FIKx1NhY8FzgYge7XXXXXXXXXXXXXXXXXXX";
file.OpenRead().Upload(uri);
}
}

有人看到我做错了什么吗?

提前谢谢您

最佳答案

该问题似乎与卡巴斯基互联网安全有关。当禁用它时,它会起作用。

关于使用 HttpWebRequest 时 Silverlight 5 插件崩溃或请求中止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9377472/

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