集册 Java实例教程 根据名称从cookie列表中提取cookie值。

根据名称从cookie列表中提取cookie值。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

689
根据名称从cookie列表中提取cookie值。

/**

 * Copyright (c) 2000-2016 Liferay, Inc. All rights reserved.

 *

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *    http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 *//*n o w  j a v a  . c o m 提供*/

//package com.nowjava;


public class Main {

    /**

     * Extracts a cookie value from a list of cookies based on the name.

     *

     * @param   cookieName  Name of the cookie to search for.

     * @param   cookies     List of cookies of the form "name1=value1; name2=value2; ...; nameN=valueN".

     *

     * @return  The cookie value stored as a String object. null if cookie not found.

     */

    public static String getCookie(String cookieName, String cookies) {

        return getParameterValue(cookieName, cookies);

    }


    /**

     * Extracts a parameter value from a list of parameters based on the name.

     *

     * @param   paramName  name of the parameter to search forn

     * @param   params     list of parameters as a String of the form: "name1=value1; name2=value2; ...; nameN=valueN".

     *

     * @return  the parameter value or <code>null</code> if not found

     */

    protected static String getParameterValue(String paramName,

            String params) {

        int paramNameLen;

        int paramsLen;//来自 时 代 J     a    v  a - nowjava.com


        // Obvious error in arguments?

        if ((paramName == null)

                || ((paramNameLen = paramName.length()) == 0)) {

            return null;

        }


        if ((params == null) || ((paramsLen = params.length()) == 0)) {

            return null;

        }


        // Try to extract the parameter from the list of parameters

        // Parameters are defined in a string of the form

        // name1=value1; name2=value2; ...; nameN=valueN

        //

        // The following parse copies the behaviour of

        // wpugcinb_GetCookieInNewBuffer in /m/wwg/src/wpu.c

        // and

        // wpdsesgcv_get_cookie_value in /m/wwg/src/wpdses.c

        //


        int index = 0;


        for (;;) {


            // First, eat all the white spaces

            while ((params.charAt(index) == ' ') && (index < paramsLen)) {

                index++;

            }


            if (params.startsWith(paramName, index)) {

                int equalsIndex = params.indexOf('=', index);


                if (equalsIndex != -1) {

                    int i = equalsIndex - 1;


                    while ((params.charAt(i) == ' ') && (i > index)) {

                        i--;

                    }


                    if (paramNameLen == (i - index + 1)) {

                        // We've found our parameter


                        // Move 1 char past '='

                        int beginIndex = equalsIndex + 1;


                        // Check for empty parameter - if so return ""

                        if (beginIndex == paramsLen) {

                            return "";

                        }


                        // Find end of token

                        int endIndex = params.indexOf(';', beginIndex);


                        if (endIndex == -1) {

                            endIndex = paramsLen - 1;

                        } else {


                            // Check for empty parameter - if so return ""

                            if (beginIndex == endIndex) {

                                return "";

                            }


                            endIndex--;

                        }


                        // Eat white spaces out in front

                        while ((params.charAt(beginIndex) == ' ')

                                && (beginIndex < endIndex)) {

                            beginIndex++;

                        }


                        // Eat white spaces out at end

                        while ((params.charAt(endIndex) == ' ')

                                && (endIndex > beginIndex)) {

                            endIndex--;

                        }


                        // Please note that substring takes the endIndex and

                        // subtracts 1

                        // from it. Since our endIndex points to where we want

                        // it to be,

                        // we need to add 1 to it so that everything works out.

                        
展开阅读全文