Java NameValuePair类使用实例

发布时间:2025-12-09 12:01:30 浏览次数:1

实例1: removeParameter

import org.apache.commons.httpclient.NameValuePair; //导入依赖的package包/类/** * Removes all parameters with the given paramName. If there is more than * one parameter with the given paramName, all of them are removed.  If * there is just one, it is removed.  If there are none, then the request * is ignored. * * @param paramName The parameter name to remove. * * @return true if at least one parameter was removed * * @throws IllegalArgumentException When the parameter name passed is null * * @since 2.0 */public boolean removeParameter(String paramName) throws IllegalArgumentException {    LOG.trace("enter PostMethod.removeParameter(String)");    if (paramName == null) {        throw new IllegalArgumentException(            "Argument passed to removeParameter(String) cannot be null");    }    boolean removed = false;    Iterator iter = this.params.iterator();    while (iter.hasNext()) {        NameValuePair pair = (NameValuePair) iter.next();        if (paramName.equals(pair.getName())) {            iter.remove();            removed = true;        }    }    return removed;} 

实例2: toString

import org.apache.commons.httpclient.NameValuePair; //导入依赖的package包/类/** * 将NameValuePairs数组转变为字符串 *  * @param nameValues * @return */protected String toString(NameValuePair[] nameValues) {    if (nameValues == null || nameValues.length == 0) {        return "null";    }    StringBuffer buffer = new StringBuffer();    for (int i = 0; i < nameValues.length; i++) {        NameValuePair nameValue = nameValues[i];        if (i == 0) {            buffer.append(nameValue.getName() + "=" + nameValue.getValue());        } else {            buffer.append("&" + nameValue.getName() + "=" + nameValue.getValue());        }    }    return buffer.toString();} 

实例3: testUpdateUserResponseAsJSON

import org.apache.commons.httpclient.NameValuePair; //导入依赖的package包/类/** * Test for SLING-1677 */@Test public void testUpdateUserResponseAsJSON() throws IOException, JsonException {testUserId = H.createTestUser();       String postUrl = HttpTest.HTTP_BASE_URL + "/system/userManager/user/" + testUserId + ".update.json";List<NameValuePair> postParams = new ArrayList<NameValuePair>();postParams.add(new NameValuePair("displayName", "My Updated Test User"));postParams.add(new NameValuePair("url", "http://www.apache.org/updated"));Credentials creds = new UsernamePasswordCredentials(testUserId, "testPwd");String json = H.getAuthenticatedPostContent(creds, postUrl, HttpTest.CONTENT_TYPE_JSON, postParams, HttpServletResponse.SC_OK);//make sure the json response can be parsed as a JSON objectJsonObject jsonObj = JsonUtil.parseObject(json);assertNotNull(jsonObj);} 

实例4: toString

import org.apache.commons.httpclient.NameValuePair; //导入依赖的package包/类/** * 将NameValuePairs数组转变为字符串 * * @param nameValues * @return */protected String toString(NameValuePair[] nameValues) {if (nameValues == null || nameValues.length == 0) {return "null";}StringBuffer buffer = new StringBuffer();for (int i = 0; i < nameValues.length; i++) {NameValuePair nameValue = nameValues[i];if (i == 0) {buffer.append(nameValue.getName() + "=" + nameValue.getValue());} else {buffer.append("&" + nameValue.getName() + "=" + nameValue.getValue());}}return buffer.toString();} 

实例5: formatCookie

import org.apache.commons.httpclient.NameValuePair; //导入依赖的package包/类/** * Return a string suitable for sending in a <tt>"Cookie"</tt> header as * defined in RFC 2965 * @param cookie a {@link org.apache.commons.httpclient.Cookie} to be formatted as string * @return a string suitable for sending in a <tt>"Cookie"</tt> header. */public String formatCookie(final Cookie cookie) {    LOG.trace("enter RFC2965Spec.formatCookie(Cookie)");    if (cookie == null) {        throw new IllegalArgumentException("Cookie may not be null");    }    if (cookie instanceof Cookie2) {        Cookie2 cookie2 = (Cookie2) cookie;        int version = cookie2.getVersion();        final StringBuffer buffer = new StringBuffer();        this.formatter.format(buffer, new NameValuePair("$Version", Integer.toString(version)));        buffer.append("; ");        doFormatCookie2(cookie2, buffer);        return buffer.toString();    } else {        // old-style cookies are formatted according to the old rules        return this.rfc2109.formatCookie(cookie);    }} 
namevaluepair
需要做网站?需要网络推广?欢迎咨询客户经理 13272073477