Authentication 将cookie字符串转换为C#或Java中的cookie对象

Authentication 将cookie字符串转换为C#或Java中的cookie对象,authentication,cookies,Authentication,Cookies,我需要自动访问一个需要身份验证的网站。我找到了这个网站http主机的cookie字符串(使用fiddler)。是否有方法将此字符串转换为cookie对象并将其传递给Webclient以通过身份验证 将Sting转换为cookie对象。为此,您需要解析字符串以获取名称、值、路径、域等 你必须这样做- String[] cArray = cookieValueIs.split(";"); for (String s : cArray) { s = s.

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

将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

,这在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;
    }