2011-11-02 53 views
1

我目前正在嘗試登錄並獲取cookie到在visual studio中使用windows phone 7 silverlight的遠程服務器。我設法登錄並獲得登錄成功的結果,但是當我嘗試將代碼放入cookie中時,它只是失敗了。由於HttpWebRequest對象錯誤的狀態,無法設置CookieContainer?

它產生錯誤「由於HttpWebRequest對象的狀態,無法設置CookieContainer」。對我的代碼「request.CookieContainer = new CookieContainer();」

任何人都可以幫助我嗎?我似乎無法找到該錯誤,並嘗試查看文檔和示例,但沒有運氣。以下是Windows Phone上的我的全碼7

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using Microsoft.Phone.Controls; 
using System.IO; 
using System.IO.IsolatedStorage; 

namespace Testing_Login_ 
{ 
    public partial class MainPage : PhoneApplicationPage 
    { 
     // Constructor 
     public MainPage() 
     { 
      InitializeComponent(); 
     } 

     private void buttonLogin_Click(object sender, RoutedEventArgs e) 
     { 
      //HttpWebRequest req = WebRequest.Create(); 
      string POST_ADDRESS = "http://mywebsite.com"; 
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(POST_ADDRESS, UriKind.Absolute)); 
      request.Method = "POST"; 
      // don't miss out this 
      request.ContentType = "application/x-www-form-urlencoded"; 
      request.BeginGetRequestStream(new AsyncCallback(RequestReady), request); 


     } 
     // Sumbit the Post Data 
     void RequestReady(IAsyncResult asyncResult) 
     { 
      HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest; 
      Stream stream = request.EndGetRequestStream(asyncResult); 

      // Hack for solving multi-threading problem 
      // I think this is a bug 
      this.Dispatcher.BeginInvoke(delegate() 
      { 
       // Send the post variables 
       StreamWriter writer = new StreamWriter(stream); 
       writer.Write("username=" + textBoxUsername.Text + "&password=" + passwordBoxSTAMP.Password); 
       writer.Flush(); 
       writer.Close(); 

       request.CookieContainer = new CookieContainer(); 

       request.BeginGetResponse(new AsyncCallback(ResponseReady), request); 
      }); 
     } 

     // Get the Result 
     void ResponseReady(IAsyncResult asyncResult) 
     { 
      HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest; 
      HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult); 

      this.Dispatcher.BeginInvoke(delegate() 
      { 
       Stream responseStream = response.GetResponseStream(); 
       StreamReader reader = new StreamReader(responseStream); 
       // get the result text 
       string result = reader.ReadToEnd(); 


       if (result == "TRUE") 
       { 
        MessageBox.Show("Login Successful!"); 

        //CookieCollection cookieValue = response.Cookies; 
        using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) 
        { 
         using (IsolatedStorageFileStream isfs = isf.OpenFile("CookieExCookies", FileMode.OpenOrCreate, FileAccess.Write)) 
         { 
          using (StreamWriter sw = new StreamWriter(isfs)) 
          { 
           foreach (Cookie cookieValue in response.Cookies) 
           { 
            sw.WriteLine("Cookie: " + cookieValue.ToString()); 
           } 
           sw.Close(); 
          } 
         } 

        } 
        //MessageBox.Show(cookieValue.ToString()); 

       } 
       else if (result == "ICRED") 
       { 
        MessageBox.Show("Username or Password incorrect!"); 
       } 
       else 
       { 
        MessageBox.Show("Unknown Error!"+result); 
       } 
      }); 
     } 
     private void ReadFromIsolatedStorage() 
     { 
      using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) 
      { 
       using (IsolatedStorageFileStream isfs = 
        isf.OpenFile("CookieExCookies", FileMode.Open)) 
       { 
        using (StreamReader sr = new StreamReader(isfs)) 
        { 
         textBoxCookies.Text = sr.ReadToEnd(); 
         sr.Close(); 
        } 
       } 

      } 
     } 

    } 
} 

回答

0

招行: -

request.CookieContainer = new CookieContainer(); 

RequestReady,並把它放在buttonLogin_Click您已經構建了它之後。

或者因爲您沒有在任何地方重新使用它。或者如果事實上要真正起作用,您需要重新使用它,然後構建並將其保存在其他地方(例如類中的字段),並將其分配給創建的每個請求對象,然後調用任何其他的請求對象(例如之前的任何對象)。 BeginGetRequestStreamBeginGetResponse

+0

對不起,目前我是編碼新手,所以可能需要你引導我。我嘗試了你的第一種方法,但它仍然不起作用。我不明白你的第二和第三種方法?這些方法是什麼意思? – TEN

相關問題