当来自 POST 的响应是重定向时,不会触发使用 HttpWebRequest 回调的 POST
我正在尝试从 Silverlight 调用 POST 操作。我的帖子由 MVC3 控制器处理,一旦完成处理 POST 数据,该控制器就会返回重定向。我使用 HttpWebRequest 对象,一切正常。
我希望能够在上传大文件时监视和报告 POST 的进度。我首先在此处实现代码的变体< /a>.
当我将 request.AllowWriteStreamBuffering 设置为 false 时,就会出现问题。我的异步回调方法从未被触发。
如果我将 POST 处理程序更改为不返回重定向,则一切都会再次按预期工作。
有谁知道如何解决这个问题?
这是我的代码
var request = (HttpWebRequest) WebRequestCreator.ClientHttp.Create(new
Uri("http://localhost:3399/items/upload"));
request.Method = "POST";
string boundary = "---------------" + DateTime.Now.Ticks;
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.AllowWriteStreamBuffering = false;
request.ContentLength = CalculatePostLength();
request.BeginGetRequestStream(asyncResult =>
{
Stream stream = request.EndGetRequestStream(asyncResult);
var writer = new StreamWriter(stream);
//Write filename
writer.WriteLine(boundarySeparator);
writer.WriteLine(nameheader);
writer.WriteLine();
writer.WriteLine(title);
//Write file
writer.WriteLine(boundarySeparator);
writer.WriteLine(fileHeader);
writer.WriteLine(contentType);
writer.WriteLine(contentLength);
writer.WriteLine();
writer.Flush();
Stream output = writer.BaseStream;
Stream input = fileToUpload.OpenRead();
var buffer = new byte[4096];
for (int size = input.Read(buffer, 0, buffer.Length);
size > 0;
size = input.Read(buffer, 0, buffer.Length))
{
output.Write(buffer, 0, size);
}
output.Flush();
writer.WriteLine();
writer.WriteLine(boundaryCompleter);
writer.Flush();
stream.Close();
request.BeginGetResponse(ReadHttpResponseCallback, request);
}, request);
I am trying to call a POST operation from Silverlight. My post is handled by an MVC3 controller, which returns a Redirect once it's finished processing the POST data. I use the HttpWebRequest object and everything works fine.
I want to be able to monitor and report the progress of the POST as it is uploading a large file. I have started by implementing a variation of the code here.
My problem occurs when I set request.AllowWriteStreamBuffering to false. My async callback method is never fired.
If I change the POST handler to not return the redirect, everything works as expected again.
Does anyone have any idea how to get around this?
Here is my code
var request = (HttpWebRequest) WebRequestCreator.ClientHttp.Create(new
Uri("http://localhost:3399/items/upload"));
request.Method = "POST";
string boundary = "---------------" + DateTime.Now.Ticks;
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.AllowWriteStreamBuffering = false;
request.ContentLength = CalculatePostLength();
request.BeginGetRequestStream(asyncResult =>
{
Stream stream = request.EndGetRequestStream(asyncResult);
var writer = new StreamWriter(stream);
//Write filename
writer.WriteLine(boundarySeparator);
writer.WriteLine(nameheader);
writer.WriteLine();
writer.WriteLine(title);
//Write file
writer.WriteLine(boundarySeparator);
writer.WriteLine(fileHeader);
writer.WriteLine(contentType);
writer.WriteLine(contentLength);
writer.WriteLine();
writer.Flush();
Stream output = writer.BaseStream;
Stream input = fileToUpload.OpenRead();
var buffer = new byte[4096];
for (int size = input.Read(buffer, 0, buffer.Length);
size > 0;
size = input.Read(buffer, 0, buffer.Length))
{
output.Write(buffer, 0, size);
}
output.Flush();
writer.WriteLine();
writer.WriteLine(boundaryCompleter);
writer.Flush();
stream.Close();
request.BeginGetResponse(ReadHttpResponseCallback, request);
}, request);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,
HttpWebRequest
类将自动遵循任何重定向。我将怀疑这是导致问题的原因。要解决此问题,请设置
httpWebRequest.AllowAutoRedirect
为false
。默认值为true
。返回响应后,您将负责检查状态代码是否为 301、302、303 或 307,检索
Location
标头的值,然后自行执行重定向。对于 Silverlight,重定向很可能与页面级导航相关,因此无论如何您都需要手动处理此问题。By default, the
HttpWebRequest
class will automatically follow any redirects. I'm going to suspect that this is causing the issue.To get around it, set
httpWebRequest.AllowAutoRedirect
tofalse
. The default istrue
.Once the response has been returned, you will then be responsible for checking if the status code is 301, 302, 303 or 307, retrieving the value of the
Location
header and then executing the redirect yourself. In the case of Silverlight, the redirect would most likely correlate to a page level navigation, so this is something you'd need to take care of manually anyway.