0

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

4

2 回答 2

0

将 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

于 2013-07-12T21:33:35.867 回答
0

这在 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;
    }
于 2013-12-06T08:10:09.417 回答