2011-06-21 56 views
2

朋友們! 請幫幫我!從SilverLight應用程序上傳HTTPWebrequest文件(爲什麼內容長度是13?)

我嘗試從Silverlight發佈文件。我用這樣的類:

public class HttpHelper 
    { 
     public WebRequest Request { get; set; } 
     public Stream Filestream { get; private set; } 

     public HttpHelper(Stream filestream) 
     {    
      Request = WebRequest.Create("http://www.mysite.com/recieve");     
      Request.Method = "POST"; 
      Request.ContentType = "application/octet-stream"; 
      Filestream = filestream; 
     } 

     private static void BeginFilePostRequest(IAsyncResult ar) 
     { 
      HttpHelper helper = ar.AsyncState as HttpHelper; 
      if (helper != null) 
      { 
       byte[] bytes = new byte[helper.Filestream.Length]; 
       int sf = helper.Filestream.Read(bytes, 0, (int)helper.Filestream.Length); 
       //helper.Request.ContentLength = bytes.Length; //It doesn't work in SL 
       using (StreamWriter writer = new StreamWriter(helper.Request.EndGetRequestStream(ar))) 
       { 
        writer.Write(bytes); 
       } 
       helper.Request.BeginGetResponse(new AsyncCallback(HttpHelper.BeginResponse), helper); 
      } 
     } 

     private static void BeginResponse(IAsyncResult ar) 
     { 
      HttpHelper helper = ar.AsyncState as HttpHelper; 
      if (helper != null) 
      { 
       HttpWebResponse response = (HttpWebResponse)helper.Request.EndGetResponse(ar); 
       if (response != null) 
       { 
        Stream stream = response.GetResponseStream(); 
        if (stream != null) 
        { 
         using (StreamReader reader = new StreamReader(stream)) 
         { 
          //anything... 
         } 
        } 
       } 
      } 
     } 

     public void PostFile() 
     {    
      this.Request.BeginGetRequestStream(new AsyncCallback(HttpHelper.BeginFilePostRequest), this); 
     } 
    } 

我流在我的Silverlight應用程序,並嘗試通過點擊呼叫PostFile提交按鈕:無文件

private void submit_button_Click(object sender, RoutedEventArgs e) 
     {   
      //...   
      HttpHelper helper = new HttpHelper(memory_stream); 
      helper.PostFile(); 
     } 

但mysite的收到請求。它只是ContentLength 13.有什麼問題?

+0

我與同樣的問題鬥爭,但在我的情況下,silverlight甚至不會對我的java web服務做任何請求。你解決了嗎? –

回答

0

嘗試在您退出using區塊之前沖洗您的作者,您還應該從EndGetRequestStream檢索到的流上致電Close

+0

謝謝!我嘗試過這個。但ContentLength也是13。這很奇怪... – greatromul

+0

我也得到了13.你是否知道這一點? – whitehawk

0

您必須刷新並配置HTTP請求流和所有下游流,然後才能正常工作。

相關問題