コード例 #1
0
        public string RenderPager()
        {
            var sb = new StringBuilder();

            PageMode = Mode.Links;

            if (!(string.IsNullOrEmpty(BaseURL)))
            {
                if (!(BaseURL.EndsWith("/")))
                {
                    BaseURL += "/";
                }
                if (!(BaseURL.StartsWith("/")))
                {
                    BaseURL = "/" + BaseURL;
                }
            }

            var qs = string.Empty;

            if (HttpContext.Current.Request.QueryString["ts"] != null)
            {
                qs = "?ts=" + HttpContext.Current.Request.QueryString["ts"];
            }

            if (PageCount > 1)
            {
                sb.Append("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" class=\"afpager\"><tr>");
                sb.Append("<td class=\"af_pager\">" + PageText + " " + CurrentPage + " " + OfText + " " + PageCount + "</td>");
                if (CurrentPage != 1)
                {
                    if (PageMode == Mode.Links)
                    {
                        if (string.IsNullOrEmpty(BaseURL))
                        {
                            sb.Append("<td class=\"af_pagernumber\" style=\"text-align:center;\"><a href=\"" + Utilities.NavigateUrl(TabID, "", BuildParams(View, ForumID, 1, TopicId)) + "\" title=\"First Page\"> &lt;&lt; </a></td>");
                            sb.Append("<td class=\"af_pagernumber\" style=\"text-align:center;\"><a href=\"" + Utilities.NavigateUrl(TabID, "", BuildParams(View, ForumID, CurrentPage - 1, TopicId)) + "\" title=\"Previous Page\"> &lt; </a></td>");
                        }
                        else
                        {
                            sb.Append("<td class=\"af_pagernumber\" style=\"text-align:center;\"><a href=\"" + BaseURL + qs + "\" title=\"First Page\"> &lt;&lt; </a></td>");
                            sb.Append("<td class=\"af_pagernumber\" style=\"text-align:center;\"><a href=\"" + BaseURL + (CurrentPage - 1) + "/" + qs + "\" title=\"Previous Page\"> &lt; </a></td>");
                        }
                    }
                    else
                    {
                        sb.Append("<td class=\"af_pagernumber\" style=\"text-align:center;\"><a href=\"javascript:" + string.Format(ClientScript, "1") + "\" title=\"First Page\"> &lt;&lt; </a></td>");
                        sb.Append("<td class=\"af_pagernumber\" style=\"text-align:center;\"><a href=\"javascript:" + string.Format(ClientScript, CurrentPage - 1) + "\" title=\"Previous Page\"> &lt; </a></td>");
                    }
                }

                int iStart;
                int iMaxPage;

                if (CurrentPage <= 3)
                {
                    iStart   = 1;
                    iMaxPage = 5;
                }
                else
                {
                    iStart   = CurrentPage - 2;
                    iMaxPage = CurrentPage + 2;
                }

                if (iMaxPage > PageCount)
                {
                    iMaxPage = PageCount;
                }

                if (iMaxPage == PageCount)
                {
                    iStart = iMaxPage - 4;
                }

                if (iStart <= 0)
                {
                    iStart = 1;
                }

                int i;
                for (i = iStart; i <= iMaxPage; i++)
                {
                    if (i == CurrentPage)
                    {
                        sb.Append("<td class=\"af_currentpage\" style=\"text-align:center;\">" + i + "</td>");
                    }
                    else
                    {
                        if (PageMode == Mode.Links)
                        {
                            if (string.IsNullOrEmpty(BaseURL))
                            {
                                sb.Append("<td class=\"af_pagernumber\" style=\"text-align:center;\"><a href=\"" + Utilities.NavigateUrl(TabID, "", BuildParams(View, ForumID, i, TopicId)) + "\">" + i + "</a></td>");
                            }
                            else
                            {
                                if (i > 1)
                                {
                                    sb.Append("<td class=\"af_pagernumber\" style=\"text-align:center;\"><a href=\"" + BaseURL + i + "/" + qs + "\">" + i + "</a></td>");
                                }
                                else
                                {
                                    sb.Append("<td class=\"af_pagernumber\" style=\"text-align:center;\"><a href=\"" + BaseURL + qs + "\">" + i + "</a></td>");
                                }
                            }
                        }
                        else
                        {
                            sb.Append("<td class=\"af_pagernumber\" style=\"text-align:center;\"><a href=\"javascript:" + string.Format(ClientScript, i) + "\">" + i + "</a></td>");
                        }
                    }

                    if (i == PageCount)
                    {
                        break;
                    }
                }

                if (CurrentPage != PageCount)
                {
                    if (PageMode == Mode.Links)
                    {
                        if (string.IsNullOrEmpty(BaseURL))
                        {
                            sb.Append("<td class=\"af_pagernumber\" style=\"text-align:center;\"><a href=\"" + Utilities.NavigateUrl(TabID, "", BuildParams(View, ForumID, CurrentPage + 1, TopicId)) + "\" title=\"Next Page\"> &gt;</a></td>");
                            sb.Append("<td class=\"af_pagernumber\" style=\"text-align:center;\"><a href=\"" + Utilities.NavigateUrl(TabID, "", BuildParams(View, ForumID, PageCount, TopicId)) + "\" title=\"Last Page\"> &gt;&gt;</a></td>");
                        }
                        else
                        {
                            sb.Append("<td class=\"af_pagernumber\" style=\"text-align:center;\"><a href=\"" + BaseURL + (CurrentPage + 1) + "/" + qs + "\" title=\"Next Page\"> &gt;</a></td>");
                            sb.Append("<td class=\"af_pagernumber\" style=\"text-align:center;\"><a href=\"" + BaseURL + PageCount + "/" + qs + "\" title=\"Last Page\"> &gt;&gt;</a></td>");
                        }
                    }
                    else
                    {
                        sb.Append("<td class=\"af_pagernumber\" style=\"text-align:center;\"><a href=\"javascript:" + string.Format(ClientScript, CurrentPage + 1) + "\" title=\"Next Page\"> &gt;</a></td>");
                        sb.Append("<td class=\"af_pagernumber\" style=\"text-align:center;\"><a href=\"javascript:" + string.Format(ClientScript, PageCount) + "\"> &gt;&gt;</a></td>");
                    }
                }

                sb.Append("</tr></table>");
            }

            return(sb.ToString());
        }
コード例 #2
0
        public RestResponse <TOut> Send <TIn, TOut>(TIn message, string urlActionPart, Method method)
            where TIn : class
            where TOut : class
        {
            string toUrl = BaseURL;

            //join urlActionPart to BaseURL
            if (!string.IsNullOrEmpty(urlActionPart))
            {
                if (!BaseURL.EndsWith("/"))
                {
                    toUrl += "/" + urlActionPart;
                }
                else
                {
                    toUrl += urlActionPart;
                }
            }

            //add message to url
            if (method == Method.Get)
            {
                if (message != null)
                {
                    var sMessage  = Serialize(message);
                    var seperator = toUrl.Contains('?') ? "&" : "?";
                    toUrl += seperator + sMessage;
                }
            }

            var request = (HttpWebRequest)WebRequest.Create(toUrl);

            request.CookieContainer = cookieContainer;

            // add credentials
            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
            {
                var credCache = new CredentialCache();

                credCache.Add(request.RequestUri, "Basic", new NetworkCredential(username, password));
                request.Credentials = credCache;
            }

            // add headers
            foreach (var header in headers)
            {
                request.Headers.Add(header.Key, header.Value);
            }


            if (method == Method.Get)
            {
                //save url for get
            }
            else
            {
                //set correct message for post
                switch (method)
                {
                case Method.Post:
                    request.Method = "POST";
                    break;

                case Method.Put:
                    request.Method = "PUT";
                    break;

                case Method.Patch:
                    request.Method = "PATCH";
                    break;

                case Method.Brew:
                    // http://www.ietf.org/rfc/rfc2324.txt
                    request.Method = "BREW";
                    break;
                }
                string sMessage;
                //if message is string, set content to form
                if (message is string)
                {
                    sMessage            = (string)(object)message;
                    request.ContentType = "application/x-www-form-urlencoded";
                }
                else
                {
                    //else set it to xml
                    sMessage            = Serialize(message);
                    request.ContentType = "text/xml";
                }

                using (var rStream = request.GetRequestStream())
                    using (var rwStream = new StreamWriter(rStream))
                    {
                        rwStream.Write(sMessage);
                        rwStream.Close();
                        rStream.Close();
                    }
            }

            request.Accept = "application/xml, */*";

            if (!string.IsNullOrEmpty(content_type))
            {
                request.ContentType = content_type;
            }

            try
            {
                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    //parse success-response
                    return(ParseResponse <TOut>(response));
                }
            }
            catch (WebException ex)
            {
                //try to parse error-response
                var response = ex.Response as HttpWebResponse;
                if (response != null)
                {
                    return(ParseResponse <TOut>(response));
                }
                throw;
            }
        }
コード例 #3
0
        public void Request(string url, HttpRequestInformation information, Stream responseStream)
        {
            if (url == null)
            {
                url = information == null ? string.Empty : information.Url;
            }
            StringBuilder stringBuilder = new StringBuilder();
            int           removeIndex   = -1;

            if (!url.StartsWith("http") && !string.IsNullOrEmpty(BaseURL))
            {
                stringBuilder.Append(BaseURL);
                if (!BaseURL.EndsWith("/"))
                {
                    stringBuilder.Append('/');
                }
                if (url.StartsWith("/"))
                {
                    removeIndex = stringBuilder.Length;
                }
            }
            stringBuilder.Append(url);
            if (removeIndex > -1)
            {
                stringBuilder.Remove(removeIndex, 1);
            }
            int  questionMarkIndex = url.IndexOf('?');
            bool hasParameter      = false;

            if (questionMarkIndex > -1)
            {
                if (questionMarkIndex == url.Length - 1)
                {
                    stringBuilder.Remove(questionMarkIndex, 1);
                }
                else
                {
                    hasParameter = true;
                }
            }
            if (information != null && information.QueryStringParameters != null && information.QueryStringParameters.Count > 0)
            {
                stringBuilder.Append(hasParameter ? '&' : '?');
                stringBuilder.Append(information.QueryStringParameters);
            }
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(stringBuilder.ToString());

            if (information != null)
            {
                if (information.FormData == null)
                {
                    httpWebRequest.Method = information.Type.ToString().ToUpperInvariant();
                }
                else
                {
                    httpWebRequest.Method = HttpRequestType.Post.ToString().ToUpperInvariant();
                }
            }
            httpWebRequest.CookieContainer = CookieContainer;
            if (OnRequest != null && !OnRequest(httpWebRequest))
            {
                return;
            }
            if (information != null && information.OnRequest != null && !information.OnRequest(httpWebRequest))
            {
                return;
            }
            byte[] buffer = new byte[BufferSize];
            if (information != null && information.FormData != null)
            {
                using (information.FormData) {
                    using (Stream stream = httpWebRequest.GetRequestStream()) {
                        while (true)
                        {
                            int length = information.FormData.Stream.Read(buffer, 0, buffer.Length);
                            if (information != null && information.OnRequesting != null && !information.OnRequesting(length))
                            {
                                break;
                            }
                            if (length < 1)
                            {
                                if (information != null && information.OnRequested != null)
                                {
                                    information.OnRequested();
                                }
                                break;
                            }
                            stream.Write(buffer, 0, length);
                        }
                    }
                }
            }
            HttpWebResponse httpWebResponse;

            try {
                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            } catch (WebException webException) {
                httpWebResponse = webException.Response as HttpWebResponse;
                if (information == null || information.OnResponseError == null)
                {
                    if (httpWebResponse == null)
                    {
                        throw;
                    }
                }
                else
                {
                    if (OnResponseError != null && !OnResponseError(httpWebResponse, webException))
                    {
                        return;
                    }
                    if (information != null && information.OnResponseError != null && !information.OnResponseError(httpWebResponse, webException))
                    {
                        return;
                    }
                }
            }
            if (httpWebResponse == null)
            {
                return;
            }
            using (httpWebResponse) {
                if (OnResponse != null && !OnResponse(httpWebResponse))
                {
                    return;
                }
                if (information != null && information.OnResponse != null && !information.OnResponse(httpWebResponse))
                {
                    return;
                }
                using (Stream stream = httpWebResponse.GetResponseStream()) {
                    while (true)
                    {
                        int length = stream.Read(buffer, 0, buffer.Length);
                        if (information != null && information.OnResponding != null && !information.OnResponding(length))
                        {
                            break;
                        }
                        if (length == 0)
                        {
                            if (OnResponded != null)
                            {
                                OnResponded(responseStream);
                            }
                            responseStream.Position = 0;
                            if (information != null && information.OnResponded != null)
                            {
                                information.OnResponded(responseStream);
                            }
                            if (OnRespondedText != null || (information != null && information.OnRespondedText != null))
                            {
                                responseStream.Position = 0;
                                using (StreamReader streamReader = new StreamReader(responseStream, Encoding)) {
                                    string text = streamReader.ReadToEnd();
                                    if (OnRespondedText != null)
                                    {
                                        OnRespondedText(text);
                                    }
                                    if (information != null && information.OnRespondedText != null)
                                    {
                                        information.OnRespondedText(text);
                                    }
                                }
                            }
                            break;
                        }
                        responseStream.Write(buffer, 0, length);
                    }
                }
            }
        }