WP7 如何上传文件并获取服务器端回复【z】

HttpWebRequest类代码:

/// <summary>
 /// 发送内容
 /// </summary>
 private byte[] bSendingFile = null;
 
  
 
 using (IsolatedStorageFileStream sendingZipStream2 = new IsolatedStorageFileStream(strZipFolderName + "/" + strZipFileName, FileMode.Open, isoStorage))
 {
   try
   {
     // 要上传的文件 
     bSendingFile = new byte[sendingZipStream2.Length];
     sendingZipStream2.Read(bSendingFile, 0, bSendingFile.Length);
 
     // 根据uri创建HttpWebRequest对象 
     HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(new Uri(F3StaticResource.SendNMSUrl, UriKind.Absolute));
     httpReq.Method = "POST";
     httpReq.ContentType = "multipart/form-data;";
 
     IAsyncResult asyncResult = httpReq.BeginGetRequestStream(new AsyncCallback(RequestStreamCallback), httpReq);
   }
   catch(Exception ex)
   {
     String strError = ex.ToString();
   }
 
 }
 
  
 
 /// <summary>
 /// 返回用于将数据写入Stream,写入数据,并发送
 /// </summary>
 /// <param name="result"></param>
 private void RequestStreamCallback(IAsyncResult result)
 {
   HttpWebRequest request = result.AsyncState as HttpWebRequest;
 
   // 返回用于将数据写入某 URI 资源的 Stream
   Stream requestStream = request.EndGetRequestStream(result);
 
   requestStream.Write(bSendingFile, 0, bSendingFile.Length);
   requestStream.Flush();
   requestStream.Close();
 
   request.BeginGetResponse(new AsyncCallback(ResponseCallback), request);
 }
 
 /// <summary>
 /// 获取最终的返回结果
 /// </summary>
 /// <param name="result"></param>
 private void ResponseCallback(IAsyncResult result)
 {
   try
   {
     HttpWebRequest request = result.AsyncState as HttpWebRequest;
     WebResponse response = request.EndGetResponse(result) as HttpWebResponse;
 
     if (response != null)
     {
       Stream responseStream = response.GetResponseStream();
       using (StreamReader streamReader = new StreamReader(responseStream))
       {
         String strResponese = streamReader.ReadToEnd();//返回内容
 
       }
 
     }
   }
   catch (Exception ex)
   {
     String strError = ex.ToString();
   }
 }

 

posted @ 2013-02-04 14:49  NSDefaultRunLoopMode  阅读(130)  评论(0编辑  收藏  举报