【问题标题】:HttpWebRequest add cookie exceptionHttpWebRequest 添加 cookie 异常
【发布时间】:2018-01-31 21:27:40
【问题描述】:

我正在尝试使用 cookie 创建 HttpWebRequest

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://myweb.com/get_my_web/");
        req.Method = "POST";

        if (req.CookieContainer == null)
        {
            req.CookieContainer = new CookieContainer();
        }

        System.Net.Cookie newCookie = new System.Net.Cookie("sessionid", AppData.sessionid,
                        "\\", "myweb.com");
        req.CookieContainer.Add(newCookie);

req.CookieContainer.Add(newCookie); 我得到这个异常:

An unhandled exception of type 'System.Net.CookieException' occurred in System.dll

Additional information: The 'Domain'='myweb.com' part of the cookie is invalid.

知道这里有什么问题吗?

【问题讨论】:

    标签: c# .net cookies httpwebrequest


    【解决方案1】:

    错误消息具有误导性 - 问题出在 cookie 路径而不是域中。 "\\" 是无效路径,我想你的意思是/

    System.Net.Cookie newCookie = new System.Net.Cookie("sessionid", AppData.sessionid,
        "/", "myweb.com");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-08
      • 1970-01-01
      • 2013-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多