我正在将一个图像文件从windows phone 7平台发布到远程服务器。为此,我首先使用photochoosertask将图像保存到隔离存储中以便进一步访问。 为了保存,我使用了以下代码:
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isf.FileExists("myimage.jpg"))
isf.DeleteFile("myimage.jpg");
using (IsolatedStorageFileStream isfs = isf.CreateFile("myimage.jpg"))
{
var bmp = new WriteableBitmap(56, 56);
bmp.SaveJpeg(isfs, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
}
}
之后我使用以下代码发布文件:
private void UploadFilesToRemoteUrl(string url, string files, string logpath1)
{
long length = 0;
string boundary = "----------------------------" +
DateTime.Now.Ticks.ToString("x");
HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;
httpWebRequest2.Method = "POST";
// httpWebRequest2.KeepAlive = true;
//httpWebRequest2.Credentials =
//System.Net.CredentialCache.DefaultCredentials;
Stream memStream = new System.IO.MemoryStream();
byte[] boundarybytes = System.Text.Encoding.UTF8.GetBytes("\r\n--" +
boundary + "\r\n");
memStream.Write(boundarybytes, 0, boundarybytes.Length);
length += boundarybytes.Length;
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\";filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";
string header = string.Format(headerTemplate, "file", files);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
memStream.Write(headerbytes, 0, headerbytes.Length);
length += headerbytes.Length;
char[] ch = new char[1];
ch[0] = '\\';
string[] flname = filename.Split(ch);
// FileStream fileStream = new FileStream(files, FileMode.Open, FileAccess.Read);
var store = IsolatedStorageFile.GetUserStoreForApplication();
//IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(flname[6], FileMode.Open, store);
System.IO.IsolatedStorage.IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(flname[6], FileMode.Open, store);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
memStream.Write(buffer, 0, bytesRead);
length += bytesRead;
}
memStream.Write(boundarybytes, 0, boundarybytes.Length);
length += boundarybytes.Length;
fileStream.Close();
//httpWebRequest2.ContentLength = memStream.Length;
Stream requestStream = httpWebRequest2.GetRequestStream();
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
WebResponse webResponse2 = httpWebRequest2.GetResponse();
Stream stream2 = webResponse2.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
//MessageBox.Show(reader2.ReadToEnd());
webResponse2.Close();
httpWebRequest2 = null;
webResponse2 = null;
}
但是我收到回复的错误,我从WebResponse webResponse2 = httpWebRequest2.GetResponse();
得到的回复为空。
请帮帮我。
答案 0 :(得分:1)
你必须异步做任何事情。这是一个小例子:
public WebrequestExample()
{
string url = "COPY YOUR URL HERE";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
// ...
// Different settings, like content type or method...
// ...
request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
}
public void GetRequestStreamCallback(IAsyncResult asyncResult)
{
HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
Stream postStream = request.EndGetRequestStream(asyncResult);
// Data writing
// ...
// postStream.Write(...)
// ...
// Start the async operation to get the response
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
public void GetResponseCallback(IAsyncResult asyncResult)
{
HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult); // Get the HttpWebResponse
using (var stream = response.GetResponseStream())
{
using (var reader = new StreamReader(stream))
{
// Do something with the response
}
}
}