GetCookieHeader() public method

public GetCookieHeader ( Uri uri ) : string
uri Uri
return string
        public TrackemonSession FindSessionId()
        {
            var trackemonSession = new TrackemonSession();
            try
            {
                var cookieContainer = new CookieContainer();
                const string homepageUrl = "https://www.trackemon.com";
                var request = WebRequest.CreateHttp(homepageUrl);
                request.Method = "GET";
                request.Timeout = Timeout;
                request.CookieContainer = cookieContainer;
                using (var response = request.GetResponse())
                {
                    var cookieHeader = cookieContainer.GetCookieHeader(new Uri("https://www.trackemon.com"));
                    trackemonSession.cookieHeader = cookieHeader;
                    using (var reader = new StreamReader(response.GetResponseStream()))
                    {
                        string line;

                        while ((line = reader.ReadLine()) != null)
                        {
                            var match = Regex.Match(line, @"var\s+sessionId\s*=\s*\'(1?.*)\'\s*;");
                            if (match.Success)
                            {
                                trackemonSession.sessionId = match.Groups[1].Value;
                                return trackemonSession;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Log.Debug("Error trying to get a sessionId for Trackemon: {0}", e.Message);
            }
            return null;
        }
示例#2
0
        public static MyWebClient createWebClient(CookieContainer ccntr=null, string referer=null)
        {
            MyWebClient wbclnt = new MyWebClient();

            // Header 字段可以在发出请求报文后丢失,因此必须重新设置
            wbclnt.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
            wbclnt.Headers.Add(HttpRequestHeader.Accept, "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            wbclnt.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
            wbclnt.Headers.Add(HttpRequestHeader.AcceptLanguage, "zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4");
            wbclnt.Headers.Add(HttpRequestHeader.Referer, referer);
            wbclnt.Encoding = new UTF8Encoding();

            if (ccntr != null)
            {
                /// wbclnt.Headers.Add(HttpRequestHeader.Cookie, cookie);
                wbclnt.CookieContainer = ccntr;
                _cookie = ccntr.GetCookieHeader(new Uri(referer));
                LogHelper.info("create webclient with cookie : " + _cookie);
            }

            if (referer != null) _referer = referer;

            return wbclnt;
        }
示例#3
0
        // TODO: Refactor to smaller files. Perhaps split cookie handling into separate class.
        private static string GetCookieHeader(Uri uri, CookieContainer cookies)
        {
            string cookieHeader = null;

            Debug.Assert(cookies != null);

            string cookieValues = cookies.GetCookieHeader(uri);
            if (!string.IsNullOrEmpty(cookieValues))
            {
                cookieHeader = string.Format(CultureInfo.InvariantCulture, "{0}: {1}", HeaderNameCookie, cookieValues);
            }

            return cookieHeader;
        }
示例#4
0
        /// <summary>
        /// Logs the user in
        /// </summary>
        /// <param name="user">Reddit account username</param>
        /// <param name="pswd">Reddit account password</param>
        /// <returns>True/False depending on success of login</returns>
        public bool Login(string user, string pswd)
        {
            string postData = string.Format("api_type=json&user={0}&passwd={1}", user, pswd);
            string loginURI = m_domain + string.Format(APIPaths.login, user);
            Hashtable response = SendPOST(postData, loginURI);
            m_usr = user;

            m_errors = GetErrorsFromRedditJson(response);
            //First check for errors. Should always contain errors key.
            if (m_errors != "" )
            {
                return false;
            }
            //Only need the data segment
            Hashtable data = ((Hashtable)((Hashtable)response["json"])["data"]);
            m_modhash = data["modhash"].ToString();
            string cookieval = data["cookie"].ToString();

            redditCookie = new CookieContainer();
            Uri cookieuri = new Uri(m_domain + string.Format(APIPaths.login, user));
            redditCookie.Add(cookieuri, new Cookie("reddit_session", cookieval.Replace(",", "%2c").Replace(":", "%3A"), "/", "reddit.com"));
            jsonGet.Headers["cookie"] = redditCookie.GetCookieHeader(cookieuri);
            jsonGet.Headers["Useragent"] = m_useragent;
            m_logged_in = true;
            return true;
        }
示例#5
0
        //Adapted from http://www.codeproject.com/Articles/330142/Cookie-Quest-A-Quest-to-Read-Cookies-from-Four-Pop
        private static string GetAllCookies_FireFox(string strHost)
        {
            string strPath, strTemp, strDb;
            strTemp = string.Empty;

            // Check to see if FireFox Installed
            strPath = GetFireFoxCookiePath();
            if (string.Empty == strPath) // Nope, perhaps another browser
                return null;

            try
            {
                // First copy the cookie jar so that we can read the cookies 
                // from unlocked copy while
                // FireFox is running
                strTemp = strPath + ".temp";
                strDb = "Data Source=" + strTemp + ";pooling=false";

                File.Copy(strPath, strTemp, true);

                // Now open the temporary cookie jar and extract Value from the cookie if
                // we find it.
                using (SQLiteConnection conn = new SQLiteConnection(strDb))
                {
                    using (SQLiteCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "SELECT name, value FROM moz_cookies WHERE host LIKE '%" +
                            strHost + "%';";

                        conn.Open();
                        CookieContainer cookies = new CookieContainer();
                        using (var reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                cookies.Add(new Cookie(reader.GetString(0), reader.GetString(1), "/", StravaDomain));
                            }
                        }
                        return cookies.GetCookieHeader(new Uri("https://" + StravaDomain));
                    }
                }
            }
            finally
            {
                // All done clean up
                if (string.Empty != strTemp)
                {
                    File.Delete(strTemp);
                }
            }
        }
示例#6
0
        public HttpResponse GetResponse(HttpRequest request, CookieContainer cookies)
        {
            if (!CheckAvailability())
            {
                throw new ApplicationException("Curl failed to initialize.");
            }

            if (request.NetworkCredential != null)
            {
                throw new NotImplementedException("Credentials not supported for curl dispatcher.");
            }

            lock (CurlGlobalHandle.Instance)
            {
                Stream responseStream = new MemoryStream();
                Stream headerStream = new MemoryStream();

                using (var curlEasy = new CurlEasy())
                {
                    curlEasy.AutoReferer = false;
                    curlEasy.WriteFunction = (b, s, n, o) =>
                    {
                        responseStream.Write(b, 0, s * n);
                        return s * n;
                    };
                    curlEasy.HeaderFunction = (b, s, n, o) =>
                    {
                        headerStream.Write(b, 0, s * n);
                        return s * n;
                    };

                    curlEasy.Url = request.Url.AbsoluteUri;
                    switch (request.Method)
                    {
                        case HttpMethod.GET:
                            curlEasy.HttpGet = true;
                            break;

                        case HttpMethod.POST:
                            curlEasy.Post = true;
                            break;

                        case HttpMethod.PUT:
                            curlEasy.Put = true;
                            break;

                        default:
                            throw new NotSupportedException(string.Format("HttpCurl method {0} not supported", request.Method));
                    }
                    curlEasy.UserAgent = UserAgentBuilder.UserAgent;
                    curlEasy.FollowLocation = request.AllowAutoRedirect;

                    if (OsInfo.IsWindows)
                    {
                        curlEasy.CaInfo = "curl-ca-bundle.crt";
                    }

                    if (cookies != null)
                    {
                        curlEasy.Cookie = cookies.GetCookieHeader(request.Url);
                    }

                    if (!request.Body.IsNullOrWhiteSpace())
                    {
                        // TODO: This might not go well with encoding.
                        curlEasy.PostFieldSize = request.Body.Length;
                        curlEasy.SetOpt(CurlOption.CopyPostFields, request.Body);
                    }

                    // Yes, we have to keep a ref to the object to prevent corrupting the unmanaged state
                    using (var httpRequestHeaders = SerializeHeaders(request))
                    {
                        curlEasy.HttpHeader = httpRequestHeaders;

                        var result = curlEasy.Perform();

                        if (result != CurlCode.Ok)
                        {
                            throw new WebException(string.Format("Curl Error {0} for Url {1}", result, curlEasy.Url));
                        }
                    }

                    var webHeaderCollection = ProcessHeaderStream(request, cookies, headerStream);
                    var responseData = ProcessResponseStream(request, responseStream, webHeaderCollection);

                    var httpHeader = new HttpHeader(webHeaderCollection);

                    return new HttpResponse(request, httpHeader, responseData, (HttpStatusCode)curlEasy.ResponseCode);
                }
            }
        }
示例#7
0
文件: Form1.cs 项目: zzzbit/Web_VS
        /**
        * Post方式请求
        **/
        private void button3_Click(object sender, EventArgs e)
        {
            Encoding myEncoding = Encoding.GetEncoding("gb2312");
            CookieContainer cookieCon = new CookieContainer();
            string Cookiesstr = string.Empty;
            string indata = HttpUtility.UrlEncode("j_username", myEncoding) + "=" + HttpUtility.UrlEncode("2120131096", myEncoding) + "&" + HttpUtility.UrlEncode("j_password", myEncoding) + "=" + HttpUtility.UrlEncode("izzz0928", myEncoding);

            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] bs = encoding.GetBytes(indata);
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://grdms.bit.edu.cn/yjs/login.do");
            myReq.CookieContainer = cookieCon;
            myReq.Method = "POST";
            myReq.UserAgent = "Mozilla/5.0 (Windows NT 5.2; rv:11.0) Gecko/20100101 Firefox/11.0";
            myReq.ContentLength = bs.Length;
            myReq.ContentType = "application/x-www-form-urlencoded";        //POST类型需要设置
            myReq.AllowAutoRedirect = false;        //禁止重定向,方便获取Cookie
            //创建输入流
            Stream dataStream = null;
            try
            {
                //把数据写入HttpWebRequest的Request流
                dataStream = myReq.GetRequestStream();
                dataStream.Write(bs, 0, bs.Length);
                //dataStream.Close();
            }
            catch (Exception)
            {
            }

            //获得响应及Cookie
            string res = string.Empty;
            try
            {
                HttpWebResponse response = (HttpWebResponse)myReq.GetResponse();
                Cookiesstr = cookieCon.GetCookieHeader(myReq.RequestUri);
                Encoding respEncoding =  Encoding.GetEncoding(response.CharacterSet);
                StreamReader reader = new StreamReader(response.GetResponseStream(),respEncoding);
                res = reader.ReadToEnd();
                reader.Close();
                response.Close();
                label1.Text = res;
                FileStream fs = new FileStream("test.html", FileMode.Create);
                StreamWriter sw = new StreamWriter(fs, respEncoding);
                sw.Write(res);
                sw.Close();
                fs.Close();
            }
            catch (Exception e1)
            {
                label1.Text = e1.Message;
            }

            //获取主页
            myReq = (HttpWebRequest)WebRequest.Create("http://grdms.bit.edu.cn/yjs/yanyuan/py/pychengji.do?method=enterChaxun");
            myReq.CookieContainer = cookieCon;
            myReq.Headers.Add("Cookie:" + Cookiesstr);
            //获得响应
            try
            {
                HttpWebResponse response = (HttpWebResponse)myReq.GetResponse();
                Cookiesstr = cookieCon.GetCookieHeader(myReq.RequestUri);
                Encoding respEncoding = Encoding.GetEncoding(response.CharacterSet);
                StreamReader reader = new StreamReader(response.GetResponseStream(), respEncoding);
                res = reader.ReadToEnd();
                reader.Close();
                response.Close();
                label1.Text = res;
                FileStream fs = new FileStream("result.html", FileMode.Create);
                StreamWriter sw = new StreamWriter(fs, respEncoding);
                sw.Write(res);
                sw.Close();
                fs.Close();
            }
            catch (Exception)
            {
            }
        }
        public static string GetWebData(string url, CookieContainer cc = null, string referer = null, IWebProxy proxy = null, bool forceUTF8 = false, bool allowUnsafeHeader = false, string userAgent = null, Encoding encoding = null)
        {
            HttpWebResponse response = null;
            try
            {
                string requestCRC = Helpers.EncryptionUtils.CalculateCRC32(string.Format("{0}{1}{2}{3}{4}", url, referer, userAgent, proxy != null ? proxy.GetProxy(new Uri(url)).AbsoluteUri : "", cc != null ? cc.GetCookieHeader(new Uri(url)) : ""));

                // try cache first
                string cachedData = WebCache.Instance[requestCRC];
                Log.Debug("GetWebData{1}: '{0}'", url, cachedData != null ? " (cached)" : "");
                if (cachedData != null) return cachedData;

                // request the data
                if (allowUnsafeHeader) Helpers.DotNetFrameworkHelper.SetAllowUnsafeHeaderParsing(true);
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                if (request == null) return "";
                if (!String.IsNullOrEmpty(userAgent))
                    request.UserAgent = userAgent; // set specific UserAgent if given
                else
                    request.UserAgent = OnlineVideoSettings.Instance.UserAgent; // set OnlineVideos default UserAgent
                request.Accept = "*/*"; // we accept any content type
                request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate"); // we accept compressed content

                request.Headers.Add("X-Requested-With: XMLHttpRequest");
                request.Headers.Add("x-addr: 127.0.0.1");

                if (!String.IsNullOrEmpty(referer)) request.Referer = referer; // set referer if given
                if (cc != null) request.CookieContainer = cc; // set cookies if given
                if (proxy != null) request.Proxy = proxy; // send the request over a proxy if given
                try
                {
                    response = (HttpWebResponse)request.GetResponse();
                }
                catch (WebException webEx)
                {
                    Log.Debug(webEx.Message);
                    response = (HttpWebResponse)webEx.Response; // if the server returns a 404 or similar .net will throw a WebException that has the response
                }
                Stream responseStream;
                if (response == null) return "";
                if (response.ContentEncoding.ToLower().Contains("gzip"))
                    responseStream = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                else if (response.ContentEncoding.ToLower().Contains("deflate"))
                    responseStream = new System.IO.Compression.DeflateStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                else
                    responseStream = response.GetResponseStream();

                // UTF8 is the default encoding as fallback
                Encoding responseEncoding = Encoding.UTF8;
                // try to get the response encoding if one was specified and neither forceUTF8 nor encoding were set as parameters
                if (!forceUTF8 && encoding == null && response.CharacterSet != null && !String.IsNullOrEmpty(response.CharacterSet.Trim())) responseEncoding = Encoding.GetEncoding(response.CharacterSet.Trim(new char[] { ' ', '"' }));
                // the caller did specify a forced encoding
                if (encoding != null) responseEncoding = encoding;
                // the caller wants to force UTF8
                if (forceUTF8) responseEncoding = Encoding.UTF8;

                using (StreamReader reader = new StreamReader(responseStream, responseEncoding, true))
                {
                    string str = reader.ReadToEnd().Trim();
                    // add to cache if HTTP Status was 200 and we got more than 500 bytes (might just be an errorpage otherwise)
                    if (response.StatusCode == HttpStatusCode.OK && str.Length > 500) WebCache.Instance[requestCRC] = str;
                    return str;
                }
            }
            finally
            {
                if (response != null) ((IDisposable)response).Dispose();
                // disable unsafe header parsing if it was enabled
                if (allowUnsafeHeader) Helpers.DotNetFrameworkHelper.SetAllowUnsafeHeaderParsing(false);
            }
        }
示例#9
0
        /// <summary>
        /// Gets a string that can be given to the MediaPortal Url Source Splitter holding the url and all parameters.
        /// </summary>
        internal override string ToFilterString()
        {
            ParameterCollection parameters = new ParameterCollection();

            if (this.IgnoreContentLength != HttpUrl.DefaultHttpIgnoreContentLength)
            {
                parameters.Add(new Parameter(HttpUrl.ParameterHttpIgnoreContentLength, this.IgnoreContentLength ? "1" : "0"));
            }
            if (this.OpenConnectionTimeout != HttpUrl.DefaultHttpOpenConnectionTimeout)
            {
                parameters.Add(new Parameter(HttpUrl.ParameterHttpOpenConnectionTimeout, this.OpenConnectionTimeout.ToString()));
            }
            if (this.OpenConnectionSleepTime != HttpUrl.DefaultHttpOpenConnectionSleepTime)
            {
                parameters.Add(new Parameter(HttpUrl.ParameterHttpOpenConnectionSleepTime, this.OpenConnectionSleepTime.ToString()));
            }
            if (this.TotalReopenConnectionTimeout != HttpUrl.DefaultHttpTotalReopenConnectionTimeout)
            {
                parameters.Add(new Parameter(HttpUrl.ParameterHttpTotalReopenConnectionTimeout, this.TotalReopenConnectionTimeout.ToString()));
            }
            if (String.CompareOrdinal(this.Referer, HttpUrl.DefaultHttpReferer) != 0)
            {
                parameters.Add(new Parameter(HttpUrl.ParameterHttpReferer, this.Referer.ToString()));
            }
            if (String.CompareOrdinal(this.UserAgent, HttpUrl.DefaultHttpUserAgent) != 0)
            {
                parameters.Add(new Parameter(HttpUrl.ParameterHttpUserAgent, this.UserAgent.ToString()));
            }

            if (this.Version == HttpVersion.Version10)
            {
                parameters.Add(new Parameter(HttpUrl.ParameterHttpVersion, HttpUrl.HttpVersionForce10.ToString()));
            }
            else if (this.Version == HttpVersion.Version11)
            {
                parameters.Add(new Parameter(HttpUrl.ParameterHttpVersion, HttpUrl.HttpVersionForce11.ToString()));
            }

            if (this.Cookies.Count > 0)
            {
                CookieContainer container = new CookieContainer(this.Cookies.Count);
                foreach (Cookie cookie in this.Cookies)
                {
                    container.Add(this.Uri, cookie);
                }
                parameters.Add(new Parameter(HttpUrl.ParameterHttpCookie, container.GetCookieHeader(this.Uri)));
            }

            if (this.CustomHeaders.Count > 0)
            {
                parameters.Add(new Parameter(HttpUrl.ParameterHttpHeadersCount, this.CustomHeaders.Count.ToString()));

                for (int i = 0; i < this.CustomHeaders.Count; i++)
                {
                    HttpHeader header = this.CustomHeaders[i];

                    parameters.Add(new Parameter(String.Format(HttpUrl.ParameterHttpHeaderFormatName, i), header.Name));
                    parameters.Add(new Parameter(String.Format(HttpUrl.ParameterHttpHeaderFormatValue, i), header.Value));
                    
                }
            }

            if (this.ServerAuthenticate)
            {
                parameters.Add(new Parameter(HttpUrl.ParameterHttpServerAuthenticate, "1"));
                parameters.Add(new Parameter(HttpUrl.ParameterHttpServerUserName, this.ServerUserName));
                parameters.Add(new Parameter(HttpUrl.ParameterHttpServerPassword, this.ServerPassword));
            }

            if (this.ProxyServerAuthenticate)
            {
                parameters.Add(new Parameter(HttpUrl.ParameterHttpProxyServerAuthenticate, "1"));
                parameters.Add(new Parameter(HttpUrl.ParameterHttpProxyServer, this.ProxyServer));
                parameters.Add(new Parameter(HttpUrl.ParameterHttpProxyServerPort, this.ProxyServerPort.ToString()));
                parameters.Add(new Parameter(HttpUrl.ParameterHttpProxyServerUserName, this.ProxyServerUserName));
                parameters.Add(new Parameter(HttpUrl.ParameterHttpProxyServerPassword, this.ProxyServerPassword));
                parameters.Add(new Parameter(HttpUrl.ParameterHttpProxyServerType, ((int)this.ProxyServerType).ToString()));
            }

            if (String.CompareOrdinal(this.StreamFileName, HttpUrl.DefaultStreamFileName) != 0)
            {
                parameters.Add(new Parameter(HttpUrl.ParameterHttpStreamFileName, this.StreamFileName.ToString()));
            }

            // return formatted connection string
            return base.ToFilterString() + ParameterCollection.ParameterSeparator + parameters.FilterParameters;
        }
示例#10
0
        /*public static string AuthImgur()
        {
            var oauth = new Manager();
            oauth["consumer_key"] = Core.ApiKeys.Imgur.Key;
            oauth["consumer_secret"] = Core.ApiKeys.Imgur.Secret;
            oauth.AcquireRequestToken("https://api.imgur.com/oauth/request_token", "POST");
            ImgurAuth imgur = new ImgurAuth(new Uri("https://api.imgur.com/oauth/authorize?oauth_token=" + oauth["token"]));
            imgur.ShowDialog();
            printTokenInfo(oauth);
            if(imgur.pin != string.Empty)
                oauth.AcquireAccessToken("https://api.imgur.com/oauth/access_token", "GET", imgur.pin);
            printTokenInfo(oauth);
            return oauth["token"] + "|" + oauth["token_secret"];
        }*/
        public static string AuthImgur(int errorcode)
        {
            CREDUI_INFO credui = new CREDUI_INFO();
            credui.pszCaptionText = "Log in to imgur.com";
            credui.pszMessageText = "If you don't got a account, create one at imgur.com";
            credui.cbSize = Marshal.SizeOf(credui);
            uint authPackage = 0;
            IntPtr outCredBuffer = new IntPtr();
            uint outCredSize;
            bool save = false;

            int result = CredUIPromptForWindowsCredentials(ref credui, errorcode, ref authPackage, IntPtr.Zero, 0, out outCredBuffer, out outCredSize, ref save, 1);

            var usernameBuf = new StringBuilder(100);
            var passwordBuf = new StringBuilder(100);
            var domainBuf = new StringBuilder(100);

            int maxUserName = 100;
            int maxDomain = 100;
            int maxPassword = 100;
            if (result == 0)
            {
                if (CredUnPackAuthenticationBuffer(0, outCredBuffer, outCredSize, usernameBuf, ref maxUserName, domainBuf, ref maxDomain, passwordBuf, ref maxPassword))
                {
                    //clear the memory allocated by CredUIPromptForWindowsCredentials
                    CoTaskMemFree(outCredBuffer);
                    HttpWebRequest wc = (HttpWebRequest)HttpWebRequest.Create(new Uri("https://imgur.com/signin"));
                    wc.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11";
                    wc.Referer = "http://imgur.com";

                    errorcode = 0;
                    NameValueCollection nvc = new NameValueCollection();
                    nvc.Add("username", usernameBuf.ToString());
                    nvc.Add("password", passwordBuf.ToString());
                    nvc.Add("remember", "remember");
                    nvc.Add("Submit", "Sign in");

                    ProgressDialog diag = new ProgressDialog(IntPtr.Zero);
                    diag.Line1 = "Authenticating with imgur...";
                    diag.Line2 = "EasyCapture is contacting the third party imgur server...";
                    diag.Line3 = " ";
                    diag.Title = "EasyCapture";
                    diag.Maximum = 1;
                    diag.CancelMessage = "Trying to stop....";
                    diag.ShowDialog();

                    diag.Value = 1337; //will create desired marquee effect

                    string resp = "";

                    HttpWebResponse web = null;
                    var parameters = new StringBuilder();
                    foreach (string key in nvc)
                    {
                        parameters.AppendFormat("{0}={1}&",
                            HttpUtility.UrlEncode(key),
                            HttpUtility.UrlEncode(nvc[key]));
                    }
                    parameters.Length -= 1;

                    wc.Method = "POST";
                    wc.ContentType = "application/x-www-form-urlencoded";
                    wc.AllowAutoRedirect = false;

                    using (var writer = new StreamWriter(wc.GetRequestStream()))
                    {
                        writer.Write(parameters.ToString());
                    }

                    try
                    {
                        List<byte> buffer = new List<byte>();
                        web = (HttpWebResponse)wc.GetResponse();
                        Stream req = web.GetResponseStream();
                        while (true)
                        {
                            int bt = req.ReadByte();
                            if (bt == -1) break;
                            buffer.Add((byte)bt);
                        }
                        resp = Encoding.Default.GetString(buffer.ToArray());
                    }
                    catch (Exception z) { Out.WriteError(z.ToString()); resp = ""; }

                            CookieContainer cc = new CookieContainer();
                            string gh = web.Headers["Set-Cookie"];
                            cc.SetCookies(new Uri("http://imgur.com"), web.Headers["Set-Cookie"]);

                    bool loc = true;
                    try
                    {
                        if (web.GetResponseHeader("Location") == string.Empty) throw new Exception("Woops");
                    }
                    catch
                    {
                        loc = false;
                    }
                    finally
                    {
                        if (loc)
                        {
                            HttpWebRequest web2 = (HttpWebRequest)HttpWebRequest.Create(new Uri(web.GetResponseHeader("Location")));
                            web2.Method = "GET";
                            web2.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11";
                            web2.Referer = "http://imgur.com";
                            web2.Headers.Set("Cookie", cc.GetCookieHeader(new Uri("http://imgur.com")));
                            try
                            {
                                Out.WriteDebug("Alternative cookie header: "+ ((HttpWebResponse)web2.GetResponse()).GetResponseHeader("Set-Cookie"));
                            }
                            catch
                            {
                                Out.WriteError("No new cookies");
                            }
                        }
                    }
                    diag.CloseDialog();

                    if (!diag.HasUserCancelled)
                    {

                        if (resp.Contains("Your login information was incorrect"))
                        {
                            errorcode = 1326;
                            return AuthImgur(errorcode);
                        }
                        else
                        {
                            TaskDialog.Show("Your imgur account has been successfully saved", "Authorization succeeded", "Yay!");

                            string header = cc.GetCookieHeader(new Uri("http://imgur.com"));
                            //wc.Headers.Add("Cookie", cc.GetCookieHeader(new Uri("http://imgur.com")));
                            //string aa = wc.DownloadString("http://api.imgur.com/2/account.json?_fake_status=200");
                            return Crypto.EncryptStringAES(header, Core.Secret);
                        }
                    }
                    else return "";
                }

                else errorcode = 1359; //an internal error occured
            }
            return "";
        }
        object CallHandlerInternal(string url, object data, HttpCookieCollection cookies)
        {
            Uri uri = new Uri(url);

            string postData;

            if (data != null)
                postData = "data=" + SessionHandler.GetSerializedString(data);
            else
                postData = "";

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);

            try
            {
                // TODO: do we need to do better cookie handling?
                CookieContainer cookieContainer = new CookieContainer();

                if (cookies != null)
                {
                    foreach (string name in cookies.AllKeys)
                    {
                        string quotedCookieValue = cookies[name].Value;

                        if (quotedCookieValue == null)
                            quotedCookieValue = "";
                        else if (quotedCookieValue.IndexOfAny(new char[] { ',', ';' }) != -1)
                            quotedCookieValue = "\"" + quotedCookieValue.Replace("\"", "\\\"") + "\"";

                        try
                        {
                            cookieContainer.Add(new Cookie(name, quotedCookieValue, "/", uri.Host));
                        }
                        catch (Exception ex)
                        {
                            // Eat it -- we mainly want the session cookies anyway
                            // TODO: log
                        }
                    }
                }

                string cookieHeader = cookieContainer.GetCookieHeader(uri);

                if (!string.IsNullOrEmpty(cookieHeader))
                    req.Headers["Cookie"] = cookieHeader;

                req.Method = "POST";
                req.ContentLength = postData.Length;
                req.ContentType = "application/x-www-form-urlencoded";

                // TODO: make this configurable
                // TODO: add better errors
                // TODO: use the polltimeout value on the control
                req.Timeout = 10000;
                req.ServicePoint.ConnectionLimit = 200;
                // TODO: pass through windows auth

                using (Stream s = req.GetRequestStream())
                using (StreamWriter w = new StreamWriter(s))
                {
                    w.Write(postData);
                }

                WebResponse resp = req.GetResponse();

                string responseData;

                using (Stream s = resp.GetResponseStream())
                using (StreamReader r = new StreamReader(s))
                {
                    responseData = r.ReadToEnd();
                }

                if (!string.IsNullOrEmpty(responseData))
                {
                    return SessionHandler.GetStringDeserialized(responseData);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
示例#12
0
		public void GetCookieHeader7a ()
		{
			CookieContainer cc = new CookieContainer ();
			Cookie cookie = new Cookie ("name1", "value1", "/path", ".mono.com");
			cookie.Comment = "Short name";
			cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
			cookie.Version = 0;
			cc.Add (cookie);
#if RFC2109
			cookie = new Cookie ("name2", "value2", "/path/sub", ".mono.com");
			cookie.Comment = "Description";
			cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
			cookie.Version = 1;
			cc.Add (cookie);
			Assert.AreEqual ("$Version=1; name2=value2; $Path=/path/sub; $Domain=.mono.com; name1=value1", cc.GetCookieHeader (new Uri ("http://live.mono.com/path/sub")), "#A1");
#endif
			Assert.AreEqual ("name1=value1", cc.GetCookieHeader (new Uri ("http://live.mono.com/path")), "#A2");
			Assert.AreEqual (string.Empty, cc.GetCookieHeader (new Uri ("http://live.mono.com/whatever")), "#A3");
			Assert.AreEqual (string.Empty, cc.GetCookieHeader (new Uri ("http://gomono.com/path/sub")), "#A4");
			Assert.AreEqual ("name1=value1", cc.GetCookieHeader (new Uri ("http://mono.com/path/sub")), "#A5");
		}
示例#13
0
        private void GetResponseCallback(IAsyncResult asyncResult)
        {
            
            HttpWebResponse response = null;
            try
            {
                response = m_webRequest.EndGetResponse(asyncResult) as HttpWebResponse;
            }
            catch (WebException e)
            {
                LOG.ERROR("EndGetResponse", e);
                response = (HttpWebResponse)e.Response;
                m_code = Convert.ToInt32(response.StatusCode);
            }
            
            Stream stream = response.GetResponseStream();
		    LOG.INFO("openInputStream done");

            CookieContainer container = new CookieContainer();
            container.Add(new Uri(m_strUrl), response.Cookies);
            m_strCookies = container.GetCookieHeader(new Uri(m_strUrl));
			
		    m_code = Convert.ToInt32(response.StatusCode);
            LOG.INFO("getResponseCode : " + m_code);

            readHeaders(m_headers);
            copyHashtable(m_OutHeaders, m_headers);

            try
            {
                if (m_code >= 400)
                {
                    LOG.ERROR("Error retrieving data: " + m_code);
                    if (m_code == Convert.ToInt32(HttpStatusCode.Unauthorized) && m_oSession != null)
                    {
                        LOG.ERROR("Unauthorize error.Client will be logged out");
                        m_oSession.logout();
                    }

                    m_strRespBody = readFully(stream, getResponseEncoding());
                    LOG.TRACE("Response body: " + m_strRespBody);
                }
                else
                {
                    long len = response.ContentLength;
                    LOG.INFO("fetchRemoteData data size:" + len);

                    m_strRespBody = readFully(stream, getResponseEncoding());
                    LOG.INFO("fetchRemoteData data readFully.");
                }
            }
            finally
            {
                stream.Close();
                response.Close();
                m_respWaitEvent.Set();
            }
        }
示例#14
0
        /// <summary>
        /// Request a resource from Reddit
        /// </summary>
        /// <param name="url"></param>
        /// <param name="json"></param>
        /// <returns></returns>
        internal HttpStatusCode Execute(out string json)
        {
            // Default empty Json
            json = "[]";
            string Data = json;
            HttpStatusCode Status = HttpStatusCode.Unused;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            if (Proxy != null)
                request.Proxy = Proxy;

            request.ServicePoint.ConnectionLimit = 100;
            request.Timeout = RequestTimeout;
            request.Method = Method;
            request.UserAgent = UserAgent;
            if (!String.IsNullOrEmpty(Cookie))
            {
                CookieContainer redditCookie = new CookieContainer();
                Uri cookieuri = new Uri(Url + User);
                redditCookie.Add(cookieuri, new Cookie("reddit_session", Cookie.Replace(",", "%2c").Replace(":", "%3A"), "/", "reddit.com"));
                request.Headers["cookie"] = redditCookie.GetCookieHeader(cookieuri);
            }

            using (var handle = new ManualResetEvent(false))
            {
                if (!string.IsNullOrEmpty(Content))
                {
                    // set the content type of the posted data
                    request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";

                    // Write the XML data to the post form, as part of the 'xml' variable
                    request.BeginGetRequestStream(ar =>
                    {
                        try
                        {
                            using (var requestStream = request.EndGetRequestStream(ar))
                            using (var writeStream = new StreamWriter(requestStream, Encoding.ASCII))
                            {
                                writeStream.Write(Content);
                            }
                        }
                        catch (Exception exp)
                        {
                            Debug.WriteLine("Request.Execute: " + exp.Message);
                            Console.WriteLine("Request.Execute: " + exp.Message);
                            throw exp;
                        }
                        finally
                        {
                            handle.Set();
                        }

                    }, new object() /* state */);
                    handle.WaitOne();
                    handle.Reset();
                }

                request.BeginGetResponse(ar =>
                {
                    try
                    {
                        var response = (HttpWebResponse)request.EndGetResponse(ar);

                        using (var receiveStream = response.GetResponseStream())
                        using (var readStream = new StreamReader(receiveStream, Encoding.ASCII))
                        {
                            Data = readStream.ReadToEnd();
                        }

                        Status = response.StatusCode;

                        // Update the cache hash & the time the URL was requested
                        // if the request was successful, that way we don't count
                        // failed requests in the 30 second limit
                        // throw new NotImplementedException();

                    }
                    catch (Exception exp)
                    {
                        Debug.WriteLine("Request.Execute: " + exp.Message);
                        Console.WriteLine("Request.Execute: " + exp.Message);
                    }
                    finally
                    {
                        handle.Set();
                    }

                }, new object() /* state */);

                // In case the first timeout doesn't work then move onto then
                // we'll let the wait request handle timeout as well
                handle.WaitOne(RequestTimeout + (RequestTimeout / 10));
            }
            json = Data;
            return Status;
        }
示例#15
0
        public string ReturnCookieString(string uid, string psw, CookieContainer cc)
        {
            if (SinaLogin(uid, psw, cc) == 0)
                return cc.GetCookieHeader(new Uri("http://weibo.com/"));

            else return null;
        }
示例#16
0
		public void GetCookieHeader1 ()
		{
			CookieContainer cc;
			Cookie cookie;

			cc = new CookieContainer ();
			cookie = new Cookie ("name1", "value1", "/path", "localhost");
			cookie.Comment = "Short name";
			cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
			cookie.Version = 0;
			cc.Add (cookie);
			cookie = new Cookie ("name2", "value2", "/path/sub", "localhost");
			cookie.Comment = "Description";
			cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
			cookie.Version = 1;
			cc.Add (cookie);
			Assert.AreEqual ("$Version=1; name2=value2; $Path=/path/sub; name1=value1", cc.GetCookieHeader (new Uri ("http://localhost/path/sub")), "#A1");
			Assert.AreEqual ("name1=value1", cc.GetCookieHeader (new Uri ("http://localhost/path")), "#A2");
			Assert.AreEqual (string.Empty, cc.GetCookieHeader (new Uri ("http://localhost/whatever")), "#A3");
		}
示例#17
0
		public void GetCookieHeader ()
		{
			CookieContainer cc = new CookieContainer ();
			Assert.Throws<ArgumentNullException> (delegate {
				cc.GetCookieHeader (null);
			}, "null");

			Uri uri = new Uri ("http://www.mono-project.com");
			Assert.AreEqual (String.Empty, cc.GetCookieHeader (uri), "none");

			Cookie c1 = new Cookie ();
			c1.Name = "1";
			cc.Add (uri, c1);
			Assert.AreEqual ("1=", cc.GetCookieHeader (uri), "one");

			Cookie c2 = new Cookie ("2", "z");
			cc.Add (uri, c2);
			Assert.AreEqual ("1=; 2=z", cc.GetCookieHeader (uri), "two");

			Cookie c3 = new Cookie ("3", "xy", "/");
			cc.Add (uri, c3);
			Assert.AreEqual ("1=; 2=z; 3=xy", cc.GetCookieHeader (uri), "three");

			c3.HttpOnly = true;
			Assert.AreEqual ("1=; 2=z; 3=xy", cc.GetCookieHeader (uri), "HttpOnly");
		}
示例#18
0
		public void GetCookieHeader2b ()
		{
			CookieContainer cc = new CookieContainer ();
			Cookie cookie = new Cookie ("Country", "Belgium", "/path", ".mono.com");
			cc.Add (cookie);
			cookie = new Cookie ("Age", "26", "/path", ".dev.mono.com");
			cc.Add (cookie);

			Assert.AreEqual ("Country=Belgium", cc.GetCookieHeader (new Uri ("http://dev.mono.com/path/ok")), "#C1");
			Assert.AreEqual ("", cc.GetCookieHeader (new Uri ("http://mono.com/path")), "#C2");
			Assert.AreEqual ("Country=Belgium", cc.GetCookieHeader (new Uri ("http://test.mono.com/path")), "#C3");
			Assert.AreEqual ("Age=26; Country=Belgium", cc.GetCookieHeader (new Uri ("http://us.dev.mono.com/path")), "#C4");
		}
示例#19
0
 public static void Main(string[] args)
 {
     Uri uri = new Uri("http://myemsl-dev5.emsl.pnl.gov/");
     CookieContainer cc = new CookieContainer();
     Authenticate a = new Authenticate();
     int res;
     Console.WriteLine("T1a");
     res = a.process(cc);
     if(res == 0)
     {
         Console.WriteLine("Cookies {0}", cc.GetCookieHeader(uri));
         Console.WriteLine("Cookie Count {0} {1}", cc.GetCookies(uri).Count, cc.Capacity);
         foreach(Cookie cookie in cc.GetCookies(uri))
         {
             Console.WriteLine("Cookie {0}", cookie);
         }
         test_auth(new Uri("http://myemsl-dev5.emsl.pnl.gov/myemsl/testauth/"), cc);
         logout(new Uri("http://myemsl-dev5.emsl.pnl.gov/myemsl/logout/"), cc);
     }
     Console.WriteLine("T2");
     Console.WriteLine(res);
 }
示例#20
0
		public void GetCookieHeader3 ()
		{
			CookieContainer cc = new CookieContainer ();
			cc.SetCookies (new Uri ("http://dev.test.mono.com/Whatever/Do"),
				"Country=Belgium; path=/Whatever; domain=mono.com;" +
				"Age=26; path=/Whatever; domain=test.mono.com," +
				"Weight=87; path=/Whatever/Do; domain=.mono.com");
			Assert.AreEqual ("Weight=87; Country=Belgium", cc.GetCookieHeader (new Uri ("http://dev.mono.com/Whatever/Do")), "#C1");
			Assert.AreEqual ("Weight=87; Country=Belgium", cc.GetCookieHeader (new Uri ("http://test.mono.com/Whatever/Do")), "#C2");
			Assert.AreEqual ("", cc.GetCookieHeader (new Uri ("http://mono.com/Whatever/Do")), "#C3");
			Assert.AreEqual ("Weight=87; Country=Belgium", cc.GetCookieHeader (new Uri ("http://us.test.mono.com/Whatever/Do")), "#C4");
		}
示例#21
0
		string GetHeaders ()
		{
			bool continue100 = false;
			if (sendChunked) {
				continue100 = true;
				webHeaders.RemoveAndAdd ("Transfer-Encoding", "chunked");
				webHeaders.RemoveInternal ("Content-Length");
			} else if (contentLength != -1) {
				if (ntlm_auth_state != NtlmAuthState.Challenge) {
					if (contentLength > 0)
						continue100 = true;

					webHeaders.SetInternal ("Content-Length", contentLength.ToString ());
				} else {
					webHeaders.SetInternal ("Content-Length", "0");
				}
				webHeaders.RemoveInternal ("Transfer-Encoding");
			} else {
				webHeaders.RemoveInternal ("Content-Length");
			}

			if (actualVersion == HttpVersion.Version11 && continue100 &&
			    servicePoint.SendContinue) { // RFC2616 8.2.3
				webHeaders.RemoveAndAdd ("Expect" , "100-continue");
				expectContinue = true;
			} else {
				webHeaders.RemoveInternal ("Expect");
				expectContinue = false;
			}

			bool proxy_query = ProxyQuery;
			string connectionHeader = (proxy_query) ? "Proxy-Connection" : "Connection";
			webHeaders.RemoveInternal ((!proxy_query) ? "Proxy-Connection" : "Connection");
			Version proto_version = servicePoint.ProtocolVersion;
			bool spoint10 = (proto_version == null || proto_version == HttpVersion.Version10);

			if (keepAlive && (version == HttpVersion.Version10 || spoint10)) {
				webHeaders.RemoveAndAdd (connectionHeader, "keep-alive");
			} else if (!keepAlive && version == HttpVersion.Version11) {
				webHeaders.RemoveAndAdd (connectionHeader, "close");
			}

			webHeaders.SetInternal ("Host", Host);
			if (cookieContainer != null) {
				string cookieHeader = cookieContainer.GetCookieHeader (actualUri);
				if (cookieHeader != "")
					webHeaders.RemoveAndAdd ("Cookie", cookieHeader);
				else
					webHeaders.RemoveInternal ("Cookie");
			}

			string accept_encoding = null;
			if ((auto_decomp & DecompressionMethods.GZip) != 0)
				accept_encoding = "gzip";
			if ((auto_decomp & DecompressionMethods.Deflate) != 0)
				accept_encoding = accept_encoding != null ? "gzip, deflate" : "deflate";
			if (accept_encoding != null)
				webHeaders.RemoveAndAdd ("Accept-Encoding", accept_encoding);

			if (!usedPreAuth && preAuthenticate)
				DoPreAuthenticate ();

			return webHeaders.ToString ();
		}
示例#22
0
		public void GetCookieHeader4 ()
		{
			CookieContainer cc = new CookieContainer ();
			Cookie cookie = new Cookie ("Height", "178", "/Whatever", "mono.com");
			cc.Add (cookie);
			cookie = new Cookie ("Town", "Brussels", "/Whatever", ".mono.com");
			cc.Add (cookie);
			cookie = new Cookie ("Income", "34445", "/Whatever/", ".test.mono.com");
			cc.Add (cookie);
			cookie = new Cookie ("Sex", "Male", "/WhateveR/DO", ".test.mono.com");
			cc.Add (cookie);
			cc.SetCookies (new Uri ("http://dev.test.mono.com/Whatever/Do/You"),
				"Country=Belgium," +
				"Age=26; path=/Whatever/Do; domain=test.mono.com," +
				"Weight=87; path=/");
			Assert.AreEqual ("Age=26; Income=34445; Town=Brussels",
				cc.GetCookieHeader (new Uri ("http://us.test.mono.com/Whatever/Do/Ok")),
				"#D");
		}
示例#23
0
 internal static object MakeRemoteCall(Uri uri, HttpCookieCollection httpCookies, byte[] encryptionKey, byte[] validationKey, 
                                     params object[] methodCall)
 {
     CookieContainer cookieContainer = new CookieContainer();
     if (httpCookies != null)
         foreach (string name in httpCookies.AllKeys)
         {
             string quotedCookieValue = httpCookies[name].Value;
             if (quotedCookieValue.IndexOfAny(new char[] {',', ';'}) != -1)
                 quotedCookieValue = "\"" + quotedCookieValue.Replace("\"", "\\\"") + "\"";
             try
             {
                 cookieContainer.Add(new Cookie(name, quotedCookieValue, "/", uri.Host));
             }
             catch (Exception ex)
             {
                 // We typically only need to use cookies that are used to identify the session
                 // so if other cookies throw exceptions, it is best to just ignore them.
                 if (log.IsDebugEnabled) log.DebugFormat("Ignore exception thrown by CookieContainer.Add(): {0}", ex);
             }
         }
     WebClient wc = new WebClient();
     byte[] responseBytes = null;
     try
     {
         wc.Headers.Add("Cookie", cookieContainer.GetCookieHeader(uri));
         string protectedRequestPayload = ObjectProtector.Protect(methodCall, encryptionKey, validationKey);
         NameValueCollection formValues = new NameValueCollection();
         formValues.Add("ProtectedPayload", protectedRequestPayload);
         responseBytes = wc.UploadValues(uri.ToString(), formValues);
     }
     catch (Exception ex)
     {
         log.Error(String.Format("Caught exception while making call to {0} at {1}", methodCall[0], uri),
                   ex);
         throw;
     }
     finally
     {
         wc.Dispose();
     }
     string protectedResponsePayload = System.Text.Encoding.ASCII.GetString(responseBytes);
     if (protectedResponsePayload != null && protectedResponsePayload.Length > 0)
     {
         object[] results = null;
         results = (object[])ObjectProtector.Unprotect(protectedResponsePayload, encryptionKey, validationKey);
         int j = 1;
         for (int i = 1; i < methodCall.Length; i++)
         {
             ICopyFromObject copyFromObject = methodCall[i] as ICopyFromObject;
             if (copyFromObject != null)
                 copyFromObject.CopyFrom(results[j++]);
         }
         return results[0];
     }
     return null;
 }
示例#24
0
		public void GetCookieHeader5b ()
		{
			CookieContainer cc = new CookieContainer ();
			Cookie cookie = new Cookie ("name1", "value1");
			cookie.Domain = "localhost";
			cookie.Comment = "Short name";
			cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
			cookie.Version = 1;
			cc.Add (cookie);
			Assert.AreEqual ("$Version=1; name1=value1; $Domain=localhost", cookie.ToString (), "#E0");
			Assert.AreEqual ("$Version=1; name1=value1; $Path=/",
				cc.GetCookieHeader (new Uri ("http://localhost/path/sub")), "#E1");
		}
示例#25
0
        private String makeClientCookie(HttpWebResponse response)
        {
            String strRes = "";

            if (response.Cookies != null)
            {
                try
                {
                    CookieContainer container = new CookieContainer();
                    container.Add(new Uri(m_strUrl), response.Cookies);
                    strRes = container.GetCookieHeader(new Uri(m_strUrl));
                }catch (Exception exc)
                {
                    LOG.WARNING("CookieContainer failed: " + exc.ToString());
                }
            }

            if (strRes != null && strRes.Length > 0)
                return strRes;

            for (int i = 0; i < response.Headers.Count; i++) 
            {
                String strName = response.Headers.AllKeys[i];
                String strValue = response.Headers[strName];

                if (strName.equalsIgnoreCase("Set-Cookie"))
                {
                    LOG.INFO("Set-Cookie: " + strValue);

                    int nSep = strValue.IndexOf(';');
                    String strVal = strValue;
                    if (nSep > 0)
                        strVal = strVal.substring(0, nSep);

                    strRes += strVal;//URI.parseCookie(strVal);
                }
            }

            return strRes;
        }
示例#26
0
		public void GetCookieHeader6 ()
		{
			CookieContainer cc = new CookieContainer ();
			Cookie cookie = new Cookie ("name1", "value1", "", "localhost");
			cookie.Comment = "Short name";
			cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
			cookie.Version = 0;
			cc.Add (cookie);
			Assert.AreEqual ("name1=value1",
				cc.GetCookieHeader (new Uri ("http://localhost/path/sub")), "#E2");
		}
示例#27
0
        public HttpResponse GetResponse(HttpRequest request, CookieContainer cookies)
        {
            if (!CheckAvailability())
            {
                throw new ApplicationException("Curl failed to initialize.");
            }

            lock (CurlGlobalHandle.Instance)
            {
                Stream responseStream = new MemoryStream();
                Stream headerStream = new MemoryStream();

                using (var curlEasy = new CurlEasy())
                {
                    curlEasy.AutoReferer = false;
                    curlEasy.WriteFunction = (b, s, n, o) =>
                    {
                        responseStream.Write(b, 0, s * n);
                        return s * n;
                    };
                    curlEasy.HeaderFunction = (b, s, n, o) =>
                    {
                        headerStream.Write(b, 0, s * n);
                        return s * n;
                    };

                    AddProxy(curlEasy, request);

                    curlEasy.Url = request.Url.FullUri;

                    switch (request.Method)
                    {
                        case HttpMethod.GET:
                            curlEasy.HttpGet = true;
                            break;

                        case HttpMethod.POST:
                            curlEasy.Post = true;
                            break;

                        case HttpMethod.PUT:
                            curlEasy.Put = true;
                            break;

                        default:
                            throw new NotSupportedException(string.Format("HttpCurl method {0} not supported", request.Method));
                    }
                    curlEasy.UserAgent = request.UseSimplifiedUserAgent ? UserAgentBuilder.UserAgentSimplified : UserAgentBuilder.UserAgent; ;
                    curlEasy.FollowLocation = request.AllowAutoRedirect;

                    if (request.RequestTimeout != TimeSpan.Zero)
                    {
                        curlEasy.Timeout = (int)Math.Ceiling(request.RequestTimeout.TotalSeconds);
                    }

                    if (OsInfo.IsWindows)
                    {
                        curlEasy.CaInfo = _caBundleFilePath;
                    }

                    if (cookies != null)
                    {
                        curlEasy.Cookie = cookies.GetCookieHeader((Uri)request.Url);
                    }

                    if (request.ContentData != null)
                    {
                        curlEasy.PostFieldSize = request.ContentData.Length;
                        curlEasy.SetOpt(CurlOption.CopyPostFields, new string(Array.ConvertAll(request.ContentData, v => (char)v)));
                    }

                    // Yes, we have to keep a ref to the object to prevent corrupting the unmanaged state
                    using (var httpRequestHeaders = SerializeHeaders(request))
                    {
                        curlEasy.HttpHeader = httpRequestHeaders;

                        var result = curlEasy.Perform();

                        if (result != CurlCode.Ok)
                        {
                            switch (result)
                            {
                                case CurlCode.SslCaCert:
                                case (CurlCode)77:
                                    throw new WebException(string.Format("Curl Error {0} for Url {1}, issues with your operating system SSL Root Certificate Bundle (ca-bundle).", result, curlEasy.Url));
                                default:
                                    throw new WebException(string.Format("Curl Error {0} for Url {1}", result, curlEasy.Url));

                            }
                        }
                    }

                    var webHeaderCollection = ProcessHeaderStream(request, cookies, headerStream);
                    var responseData = ProcessResponseStream(request, responseStream, webHeaderCollection);

                    var httpHeader = new HttpHeader(webHeaderCollection);

                    return new HttpResponse(request, httpHeader, responseData, (HttpStatusCode)curlEasy.ResponseCode);
                }
            }
        }
示例#28
0
		public void GetCookieHeader7b ()
		{
			CookieContainer cc = new CookieContainer ();
			Cookie cookie = new Cookie ("name1", "value1", "/path", "live.mono.com");
			cookie.Comment = "Short name";
			cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
			cookie.Version = 0;
			cc.Add (cookie);
			cookie = new Cookie ("name2", "value2", "/path/sub", "live.mono.com");
			cookie.Comment = "Description";
			cookie.Expires = DateTime.Now.Add (new TimeSpan (3, 2, 5));
			cookie.Version = 1;
			cc.Add (cookie);
			Assert.AreEqual ("$Version=1; name2=value2; $Path=/path/sub; name1=value1", cc.GetCookieHeader (new Uri ("http://live.mono.com/path/sub")), "#B1");
			Assert.AreEqual ("name1=value1", cc.GetCookieHeader (new Uri ("http://live.mono.com/path")), "#B2");
			Assert.AreEqual (string.Empty, cc.GetCookieHeader (new Uri ("http://live.mono.com/whatever")), "#B3");
			Assert.AreEqual (string.Empty, cc.GetCookieHeader (new Uri ("http://go.live.mono.com/path/sub")), "#B4");
			Assert.AreEqual (string.Empty, cc.GetCookieHeader (new Uri ("http://go.live.mono.com/path")), "#B5");
			Assert.AreEqual (string.Empty, cc.GetCookieHeader (new Uri ("http://go.live.mono.com/whatever")), "#B6");
			Assert.AreEqual (string.Empty, cc.GetCookieHeader (new Uri ("http://golive.mono.com/whatever")), "#B7");
		}
示例#29
0
        private string MyGetWebData(Uri uri, string postData = null, CookieContainer cookies = null, string referer = null, IWebProxy proxy = null, bool forceUTF8 = false, bool allowUnsafeHeader = false, string userAgent = null, Encoding encoding = null, NameValueCollection headers = null, bool cache = true)
        {
            // do not use the cache when doing a POST
            if (postData != null) cache = false;
            // set a few headers if none were given
            if (headers == null)
            {
                headers = new NameValueCollection();
                headers.Add("Accept", "*/*"); // accept any content type
                headers.Add("User-Agent", userAgent ?? OnlineVideoSettings.Instance.UserAgent); // set the default OnlineVideos UserAgent when none specified
            }
            if (referer != null) headers.Set("Referer", referer);
            HttpWebResponse response = null;
            try
            {
                // build a CRC of the url and all headers + proxy + cookies for caching
                string requestCRC = Helpers.EncryptionUtils.CalculateCRC32(
                    string.Format("{0}{1}{2}{3}",
                    uri.ToString(),
                    headers != null ? string.Join("&", (from item in headers.AllKeys select string.Format("{0}={1}", item, headers[item])).ToArray()) : "",
                    proxy != null ? proxy.GetProxy(uri).AbsoluteUri : "",
                    cookies != null ? cookies.GetCookieHeader(uri) : ""));

                // try cache first
                string cachedData = cache ? WebCache.Instance[requestCRC] : null;
                Log.Debug("GetWebData-{2}{1}: '{0}'", uri.ToString(), cachedData != null ? " (cached)" : "", postData != null ? "POST" : "GET");
                if (cachedData != null) return cachedData;

                // build the request
                if (allowUnsafeHeader) Helpers.DotNetFrameworkHelper.SetAllowUnsafeHeaderParsing(true);
                HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
                if (request == null) return "";
                request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; // turn on automatic decompression of both formats (adds header "AcceptEncoding: gzip,deflate" to the request)
                if (cookies != null) request.CookieContainer = cookies; // set cookies if given
                if (proxy != null) request.Proxy = proxy; // send the request over a proxy if given
                if (headers != null) // set user defined headers
                {
                    foreach (var headerName in headers.AllKeys)
                    {
                        switch (headerName.ToLowerInvariant())
                        {
                            case "accept":
                                request.Accept = headers[headerName];
                                break;
                            case "user-agent":
                                request.UserAgent = headers[headerName];
                                break;
                            case "referer":
                                request.Referer = headers[headerName];
                                break;
                            default:
                                request.Headers.Set(headerName, headers[headerName]);
                                break;
                        }
                    }
                }
                if (postData != null)
                {
                    byte[] data = encoding != null ? encoding.GetBytes(postData) : Encoding.UTF8.GetBytes(postData);
                    request.Method = "POST";
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.ContentLength = data.Length;
                    request.ProtocolVersion = HttpVersion.Version10;
                    Stream requestStream = request.GetRequestStream();
                    requestStream.Write(data, 0, data.Length);
                    requestStream.Close();
                }

                // request the data
                try
                {
                    response = (HttpWebResponse)request.GetResponse();
                }
                catch (WebException webEx)
                {
                    Log.Debug(webEx.Message);
                    response = (HttpWebResponse)webEx.Response; // if the server returns a 404 or similar .net will throw a WebException that has the response
                }
                Stream responseStream = response.GetResponseStream();

                // UTF8 is the default encoding as fallback
                Encoding responseEncoding = Encoding.UTF8;
                // try to get the response encoding if one was specified and neither forceUTF8 nor encoding were set as parameters
                if (!forceUTF8 && encoding == null && response.CharacterSet != null && !String.IsNullOrEmpty(response.CharacterSet.Trim())) responseEncoding = Encoding.GetEncoding(response.CharacterSet.Trim(new char[] { ' ', '"' }));
                // the caller did specify a forced encoding
                if (encoding != null) responseEncoding = encoding;
                // the caller wants to force UTF8
                if (forceUTF8) responseEncoding = Encoding.UTF8;

                using (StreamReader reader = new StreamReader(responseStream, responseEncoding, true))
                {
                    string str = reader.ReadToEnd().Trim();
                    // add to cache if HTTP Status was 200 and we got more than 500 bytes (might just be an errorpage otherwise)
                    if (cache && response.StatusCode == HttpStatusCode.OK && str.Length > 500) WebCache.Instance[requestCRC] = str;
                    return str;
                }
            }
            finally
            {
                if (response != null) ((IDisposable)response).Dispose();
                // disable unsafe header parsing if it was enabled
                if (allowUnsafeHeader) Helpers.DotNetFrameworkHelper.SetAllowUnsafeHeaderParsing(false);
            }
        }
示例#30
0
		public void GetCookieHeader_Uri_Null ()
		{
			CookieContainer cc = new CookieContainer ();
			try {
				cc.GetCookieHeader ((Uri) null);
				Assert.Fail ("#1");
			}
			catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.AreEqual ("uri", ex.ParamName, "#5");
			}
		}
示例#31
0
        public string Login(string username, string password)
        {
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) return "";

            try
            {
                CookieContainer container = new CookieContainer();
                string formUrl = "http://bakabt.me/login.php"; // NOTE: This is the URL the form POSTs to, not the URL of the form (you can find this in the "action" attribute of the HTML's form tag
                string formParams = string.Format("username={0}&password={1}", username, password);

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(formUrl);
                req.ContentType = "application/x-www-form-urlencoded";
                req.Method = "POST";
                //req.AllowAutoRedirect = false;
                req.CookieContainer = container;
                req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
                req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
                req.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                byte[] bytes = Encoding.ASCII.GetBytes(formParams);
                req.ContentLength = bytes.Length;
                using (Stream os = req.GetRequestStream())
                {
                    os.Write(bytes, 0, bytes.Length);
                }

                HttpWebResponse WebResponse = (HttpWebResponse)req.GetResponse();

                Stream responseStream = WebResponse.GetResponseStream();
                String enco = WebResponse.CharacterSet;
                Encoding encoding = null;
                if (!String.IsNullOrEmpty(enco))
                    encoding = Encoding.GetEncoding(WebResponse.CharacterSet);
                if (encoding == null)
                    encoding = Encoding.Default;
                StreamReader Reader = new StreamReader(responseStream, encoding);

                string output = Reader.ReadToEnd();

                if (container.Count < 3)
                    return "";

                //Grab the cookie we just got back for this specifc page
                return container.GetCookieHeader(new Uri("http://www.bakabt.me/index.php"));
            }
            catch (Exception ex)
            {
                BaseConfig.MyAnimeLog.Write("Login: "******"";
            }
        }
示例#32
0
        string GetHeaders()
        {
            bool continue100 = false;

            if (contentLength != -1)
            {
                if (contentLength > 0)
                {
                    continue100 = true;
                }
                webHeaders.SetInternal("Content-Length", contentLength.ToString());
                webHeaders.RemoveInternal("Transfer-Encoding");
            }
            else if (sendChunked)
            {
                continue100 = true;
                webHeaders.RemoveAndAdd("Transfer-Encoding", "chunked");
                webHeaders.RemoveInternal("Content-Length");
            }

            if (actualVersion == HttpVersion.Version11 && continue100 &&
                servicePoint.SendContinue)               // RFC2616 8.2.3
            {
                webHeaders.RemoveAndAdd("Expect", "100-continue");
                expectContinue = true;
            }
            else
            {
                webHeaders.RemoveInternal("Expect");
                expectContinue = false;
            }

            string connectionHeader = (ProxyQuery) ? "Proxy-Connection" : "Connection";

            webHeaders.RemoveInternal((!ProxyQuery) ? "Proxy-Connection" : "Connection");
            bool spoint10 = (servicePoint.ProtocolVersion == null ||
                             servicePoint.ProtocolVersion == HttpVersion.Version10);

            if (keepAlive && (version == HttpVersion.Version10 || spoint10))
            {
                webHeaders.RemoveAndAdd(connectionHeader, "keep-alive");
            }
            else if (!keepAlive && version == HttpVersion.Version11)
            {
                webHeaders.RemoveAndAdd(connectionHeader, "close");
            }

            webHeaders.SetInternal("Host", actualUri.Authority);
            if (cookieContainer != null)
            {
                string cookieHeader = cookieContainer.GetCookieHeader(actualUri);
                if (cookieHeader != "")
                {
                    webHeaders.SetInternal("Cookie", cookieHeader);
                }
            }

            if (!usedPreAuth && preAuthenticate)
            {
                DoPreAuthenticate();
            }

            return(webHeaders.ToString());
        }