private HttpWebResponse ConsumerHandshake(OAuthRequest client, string jsessionid, string setCookieHeader, string domain)
        {
            var authReq =
                (HttpWebRequest)
                WebRequest.Create(serviceConfig.ServerRoot + AcsApiClientConfig.AuthUrl + "?" + OauthTokenKey + "=" +
                                  HttpUtility.UrlEncode(client.Token));

            authReq.AllowAutoRedirect = false;
            authReq.CookieContainer   = new CookieContainer();

            authReq.CookieContainer.Add(new Cookie(SessionIdKey.ToUpper(), jsessionid)
            {
                Domain = domain
            });
            authReq.CookieContainer.Add(new Cookie(ConsumerTypeIdKey, ConsumerTypeValue)
            {
                Domain = domain
            });

            cookies.Add(new Cookie(ConsumerTypeIdKey, ConsumerTypeValue, "/", domain));

            CookieCollection newRequiredCookies = new CookieCollection();

            try
            {
                newRequiredCookies = AcsApiSetCookieHeaderParser.GetAllCookiesFromHeader(setCookieHeader,
                                                                                         new Uri(serviceConfig.ServerRoot).Host);
            }
            catch (Exception e)
            {
                throw new AcsApiException("Could not parse cookie for final request!");
            }
            RefreshCookies(newRequiredCookies, authReq);

            HttpWebResponse authReqResp;

            try
            {
                authReqResp = authReq.GetResponse() as HttpWebResponse;
            }
            catch (WebException wex)
            {
                throw new AcsApiException(AcsApiError.ServerError.ToString(), wex);
            }

            // bbax: response streams being closed while dealing with exceptions... yey..
            // todo: clean this up more...
            if (authReqResp?.Headers == null)
            {
                authReqResp?.Close();
                throw new AcsApiException(AcsApiError.CouldNotLogin);
            }
            return(authReqResp);
        }
        private HttpWebResponse RequestFinal(OAuthRequest client, string accessToken, string oauthVerifier, string cdomain, string consumerCookies)
        {
            client.Token       = accessToken;
            client.Type        = OAuthRequestType.AccessToken;
            client.Verifier    = oauthVerifier;
            client.RequestUrl  = serviceConfig.ServerRoot + AcsApiClientConfig.AccessUrl;
            client.CallbackUrl = null;
            client.TokenSecret = HttpUtility.UrlDecode(client.TokenSecret);
            var auth = client.GetAuthorizationHeader();

            var accessTokenRequest = (HttpWebRequest)WebRequest.Create(serviceConfig.ServerRoot + AcsApiClientConfig.AccessUrl);

            accessTokenRequest.Method = "GET";
            accessTokenRequest.Headers.Add("Authorization", auth);
            accessTokenRequest.CookieContainer = new CookieContainer();
            //accessTokenRequest.CookieContainer.Add(new Cookie(ConsumerTypeIdKey, ConsumerTypeValue) { Domain = cdomain });

            CookieCollection newRequiredCookies = new CookieCollection();

            try
            {
                newRequiredCookies = AcsApiSetCookieHeaderParser.GetAllCookiesFromHeader(consumerCookies,
                                                                                         new Uri(serviceConfig.ServerRoot).Host);
            }
            catch (Exception e)
            {
                throw new AcsApiException("Could not parse cookie for final request!");
            }
            RefreshCookies(newRequiredCookies, accessTokenRequest);

            accessTokenRequest.AllowAutoRedirect = false;

            HttpWebResponse accessTokenResponse = null;

            try
            {
                accessTokenResponse = (HttpWebResponse)accessTokenRequest.GetResponse();
            }
            catch (WebException wex)
            {
                throw new AcsApiException(AcsApiError.ServerError.ToString(), wex);
            }
            return(accessTokenResponse);
        }
        private HttpWebRequest Login(OAuthRequest client, string responseCookies, Hashtable fields)
        {
            client.Token       = fields[OauthTokenKey].ToString();
            client.TokenSecret = fields[OauthTokenSecretKey].ToString();

            InvokeLog("Token and Sec: " + client.Token + " : " + client.TokenSecret);

            var ascii    = new ASCIIEncoding();
            var postData =
                ascii.GetBytes("j_username="******"&j_password="******"POST";
            myHttpWebRequest.AllowAutoRedirect = false;
            myHttpWebRequest.ContentType       = "application/x-www-form-urlencoded";
            myHttpWebRequest.ContentLength     = postData.Length;
            myHttpWebRequest.CookieContainer   = new CookieContainer();

            CookieCollection newRequiredCookies = new CookieCollection();

            try
            {
                newRequiredCookies = AcsApiSetCookieHeaderParser.GetAllCookiesFromHeader(responseCookies,
                                                                                         new Uri(serviceConfig.ServerRoot).Host);
            }
            catch (Exception e)
            {
                throw new AcsApiException("Could not parse cookie for final request!");
            }
            RefreshCookies(newRequiredCookies, myHttpWebRequest);

            using (var requestStream = myHttpWebRequest.GetRequestStream())
            {
                requestStream.Write(postData, 0, postData.Length);
                requestStream.Close();
            }
            return(myHttpWebRequest);
        }