I am posting an image file from the windows phone 7 platform to a remote server.For that firstly I have saved the开发者_高级运维 image to the isolated storage using photochoosertask for further accessing. for saving I have used the following code:
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);
}
}
After that I am using the following code to post the file:
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;
}
But I am getting the error in getting the response, I am getting null in response from WebResponse webResponse2 = httpWebRequest2.GetResponse();
.
Please help me out.
You have to do anything asynchronously. Here is a little example:
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
}
}
}
精彩评论