为什么在再次调用相同的Httprequest函数时会出现“Cookies禁用”?

时间:2018-04-02 14:32:59

标签: c# cookies httpwebrequest

为了解决“Cookies已禁用”问题,我使用了                         CookieContainer myContainer = new CookieContainer();                         request.CookieContainer = myContainer; 这在首次调用getIDfromWeb函数时有效。 但是,再次调用getIDfromWeb函数时,会出现“Cookies disabled”。 我该如何解决这个问题? 要重现相同的问题,当您不包含“request.CookieContainer = myContainer”时,您需要满足“Cookies已禁用”问题。否则您的网址可能不需要身份验证。 虽然我可以通过将我的URL放在IE地址栏上并按Enter键来获取信息,但是在从C#实现时遇到了授权错误。我使用httpwebrequest和cookie来解决问题但发现我第二次调用该函数时遇到了“Cookies禁用”问题。

       using System;
        using System.Collections.Generic;
        using System.IO;
        using System.Linq;
        using System.Net;
        using System.Text;
        using System.Threading.Tasks;

        namespace ConsoleApplication3
        {
            class Program
            {
                const string baseURL = "http://intranet/rest/reserveid.php";
                 static void Main(string[] args)
                {

                    Console.WriteLine("Key1 sample:");
                    Console.WriteLine(getIDfromWeb("key1"));
                    Console.WriteLine("key1. sample2:");
                    Console.WriteLine(getIDfromWeb("key1"));
                    Console.ReadKey();
                }
                static string getIDfromWeb(string idType)
                {
                    int startPos = 0;
                    string url = "";
                    switch (idType)
                    {
                        case "key1":
                            startPos = 19;
                            url = baseURL + "?querystringforkey1";
                            break;
                        case "key2":
                            startPos = 15;
                            url = baseURL + "?querystringforkey2";
                            break;
                    }

                    CookieContainer myContainer = new CookieContainer();
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                    request.Credentials = CredentialCache.DefaultNetworkCredentials;
                    request.CookieContainer = myContainer;
                    request.PreAuthenticate = true;
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    var dataStream = response.GetResponseStream();
                    // Open the stream using a StreamReader for easy access.
                    StreamReader reader = new StreamReader(dataStream);
                    // Read the content.
                    string responseFromServer = reader.ReadToEnd();
                    // Display the content.
                    reader.Close();
                    response.Close();
                    return responseFromServer.Substring(startPos, (responseFromServer.IndexOf("}]") - startPos - 1));

                }
            }
        }

1 个答案:

答案 0 :(得分:1)

它为我工作。

此处更新了您的代码,请检查。

static void Main(string[] args)
    {
        Console.WriteLine("Key1 sample:");
        Console.WriteLine(getIDfromWeb("key1"));
        Console.WriteLine("key1. sample2:");
        Console.WriteLine(getIDfromWeb("key2"));
        Console.ReadKey();
    }

替换下面的行

Console.WriteLine(getIDfromWeb("key2"));