1

登录网络服务后,我无法取回 cookie。我能够连接,并得到一个登录成功的布尔值(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("");

登录到网络服务

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";
4

1 回答 1

1

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

于 2012-11-03T15:56:48.710 回答