无法从webservice获取cookie

时间:2012-11-03 07:35:52

标签: c# web-services

登录网络服务后,我无法恢复cookie。 我能够连接,并获得一个bool,登录成功(infoservice.LoginToVault)。 但是,当我试图检查我是否已连接(infoservice.IsConnectedToVault)时,我得到了错误的回复。

我发现原因是因为没有保存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("");

登录webservice

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 个答案:

答案 0 :(得分:1)

我查看了你的代码,我不认为你的代码中存在任何错误。我这里有一个问题   1)您的网络服务是由您开发的吗? 如果没有请在SoupUI的帮助下测试这个web服务。如果是,请检查从IsConnectedToVault方法返回的值是什么。