我想從Silverlight調用POST操作。我的文章是由一個MVC3控制器處理的,該控制器在完成處理POST數據後返回一個重定向。我使用HttpWebRequest對象,一切正常。POST使用HttpWebRequest回調不會觸發POST時的響應是重定向
我希望能夠在上載大文件時監視和報告POST的進度。我已經開始實施代碼here的變體。
我將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);
您是否嘗試過使用瀏覽器HTTP堆棧而不是客戶端堆棧?在SL4中有一個開放的bug。 – jv42 2011-11-03 13:47:08