2012-11-03 88 views
1

我在登錄web服務後無法獲取cookie。 我可以連接,並獲得登錄成功的布爾(infoservice.LoginToVault)。 但是,當我試圖檢查我是否連接(infoservice.IsConnectedToVault)我得到一個虛假的回。無法從web服務獲取cookie

我發現原因是因爲沒有保存cookie。所以我嘗試使用cookiecontainer,但有些事告訴我,我是錯誤的軌道..我想我必須要求在登錄時的cookie,但如何?

任何幫助,將不勝感激!

 InfoService infoservice = new InfoService(); // that's the webservice 

     bool login = infoservice.LoginToVault("server", "vault", "user", "pass"); 
     bool check = infoservice.IsConnectedToVault(); 

     if (login == true) 
     { 
      Console.WriteLine("Succesfully logged in!"); 
     } 
     if (login == false) 
     { 
      Console.WriteLine("Login failed!"); 
     } 

     Console.WriteLine(infoservice.IsConnectedToVault()); // returns fallse.. 

     Uri siteUri = new Uri("http://ironman/CadacWebservice/InfoService.asmx"); 

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(siteUri); 
     request.CookieContainer = new CookieContainer(); 

     HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

     foreach (Cookie cook in response.Cookies) 
     { 
      Console.WriteLine("Cookie:"); 
      Console.WriteLine("{0} = {1}", cook.Name, cook.Value); 
      Console.WriteLine("Domain: {0}", cook.Domain); 
      Console.WriteLine("Path: {0}", cook.Path); 
      Console.WriteLine("Port: {0}", cook.Port); 
      Console.WriteLine("Secure: {0}", cook.Secure); 

      Console.WriteLine("When issued: {0}", cook.TimeStamp); 
      Console.WriteLine("Expires: {0} (expired? {1})", 
       cook.Expires, cook.Expired); 
      Console.WriteLine("Don't save: {0}", cook.Discard); 
      Console.WriteLine("Comment: {0}", cook.Comment); 
      Console.WriteLine("Uri for comments: {0}", cook.CommentUri); 
      Console.WriteLine("Version: RFC {0}", cook.Version == 1 ? "2109" : "2965"); 

      Console.WriteLine("String: {0}", cook.ToString()); 
      Console.ReadLine(); 
     } 

我能得到我的手一些Perl代碼,這我知道它的工作原理: 但現在我需要在C#。

登錄:

$soap = SOAP::Lite->new(proxy => 'http://localhost/Webservice/InfoService.asmx'); 

$soap->on_action(sub {sprintf '%s%s', @_}); 

$soap->default_ns('http://x/webservices/'); 

$soap->uri('http://x.com/webservices/'); 

$soap->proxy('http://localhost/Webservice/InfoService.asmx', keep_alive => 1, cookie_jar => 
HTTP::Cookies->new(ignore_discard => 1)); 

$soap->encodingStyle(""); 

登錄到Web服務

my $som = $soap->call('LoginToVault', 

       SOAP::Data->name('server')->value('server')->type(''), 

       SOAP::Data->name('vault')->value('vault')->type(''), 

       SOAP::Data->name('user')->value('user')->type(''), 

       SOAP::Data->name('password')->value('pass')->type('') 
); 

my $result = $som->result, "\n"; 

回答

1

我通過您的代碼了,我不這麼認爲任何錯誤是那裏我們的代碼。我在這裏有一個問題 1)那個web服務是由你開發的嗎? 如果沒有請在SoupUI的幫助下測試這個web服務。如果是,請檢查您從IsConnectedToVault方法返回的值。

+1

我沒有在app.config文件中啓用cookie。愚蠢的愚蠢...... – Marcel