【问题标题】:Convert cookie string to a Cookie object in C# or Java在 C# 或 Java 中将 cookie 字符串转换为 Cookie 对象
【发布时间】:2013-12-06 16:10:09
【问题描述】:

我需要自动访问需要身份验证的网站。我找到了这个网站的 http 主机的 cookie 字符串(使用 fiddler)。有没有办法将此字符串转换为 cookie 对象并将其传递给 Webclient 以通过身份验证?

【问题讨论】:

    标签: authentication cookies


    【解决方案1】:

    将 Sting 转换为 cookie 对象。为此,您需要解析字符串以获取名称、值、路径、域等。

    你必须这样做 -

    String[] cArray = cookieValueIs.split(";");
            for (String s : cArray) {
                    s = s.trim();
                    int i1 = s.indexOf('=');
                    if (i1 != -1) {
                            String k = s.substring(0, i1).trim();
                            String v = s.substring(i1 + 1).trim();
                            if (k.equalsIgnoreCase(VERSION)) {
                                    version = v;
                            } else if (k.equalsIgnoreCase(COMMENT)) {
                                    comment = v;
                            } else if (k.equalsIgnoreCase(DOMAIN)) {
                                    domain = v;
                            } else if (k.equalsIgnoreCase(PATH)) {
                                    path = v;
                            } else if (k.equalsIgnoreCase(MAX_AGE)) {
                                    maxAge = v;
                            } else if(k.equalsIgnoreCase(EXPIRES)){
                                    continue;
                            }
                            else {
                                    key = k;
                                    value = v;
                            }
                    } else {
                            if (s.equalsIgnoreCase(SECURE)) {
                                    secure = true;
                            } else if (s.equalsIgnoreCase(HTTPONLY)) {
                                    httpOnly = true;
                            }
                    }
    

    完成此操作后,创建一个 cookie 对象-

        Cookie cookie = new Cookie(key,value);
    if(comment != null){
                        cookie.setComment(comment);
                }
                if(domain != null){
                        cookie.setDomain(domain);
                }
                if(path != null){
                        cookie.setPath(path);
                }
                if(version != null){
                        cookie.setVersion(Integer.parseInt(version));
                }
    
                if(secure){
                        cookie.setSecure(true);
    

    现在你的字符串被转换为 Cookie 对象 --> cookie

    【讨论】:

      【解决方案2】:

      这在 c# 中对我有用。

         public static Cookie ToCookie(this string @this)
          {
              String[] array = @this.Split(';');
              var cookie = new Cookie();
              foreach (var ss in array)
              {
                  string key;
                  object value;
                  var s = ss.Trim();
                  int indexOf = s.IndexOf('=');
      
                  if (indexOf != -1) {
                      key = s.Substring(0, indexOf).Trim();
                      value = s.Substring(indexOf + 1).Trim();
                  } else
                  {
                      key = s.ToTitleCase();
                      value = true;
                  }
      
                  var prop = cookie.GetType().GetProperty(key.ToTitleCase());
                  if (prop != null)
                  {
                      var converted = Convert.ChangeType(value, prop.PropertyType);
                      prop.SetValue(cookie, converted, null);
                  }else
                  {
                      cookie.Name = key;
                      cookie.Value = value.ToString();
                  }
              }
              return cookie;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-12
        • 1970-01-01
        • 2011-02-02
        • 2016-01-06
        • 2011-06-19
        • 1970-01-01
        • 2021-12-29
        相关资源
        最近更新 更多