|
Java example source code file (URLEncodedUtils.java)
The URLEncodedUtils.java example source code/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.client.utils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import org.apache.http.annotation.Immutable;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
/**
* A collection of utilities for encoding URLs.
*
* @since 4.0
*/
@Immutable
public class URLEncodedUtils {
public static final String CONTENT_TYPE = "application/x-www-form-urlencoded";
private static final String PARAMETER_SEPARATOR = "&";
private static final String NAME_VALUE_SEPARATOR = "=";
/**
* Returns a list of {@link NameValuePair NameValuePairs} as built from the
* URI's query portion. For example, a URI of
* http://example.org/path/to/file?a=1&b=2&c=3 would return a list of three
* NameValuePairs, one for a=1, one for b=2, and one for c=3.
* <p>
* This is typically useful while parsing an HTTP PUT.
*
* @param uri
* uri to parse
* @param encoding
* encoding to use while parsing the query
*/
public static List <NameValuePair> parse (final URI uri, final String encoding) {
List <NameValuePair> result = Collections.emptyList();
final String query = uri.getRawQuery();
if (query != null && query.length() > 0) {
result = new ArrayList <NameValuePair>();
parse(result, new Scanner(query), encoding);
}
return result;
}
/**
* Returns a list of {@link NameValuePair NameValuePairs} as parsed from an
* {@link HttpEntity}. The encoding is taken from the entity's
* Content-Encoding header.
* <p>
* This is typically used while parsing an HTTP POST.
*
* @param entity
* The entity to parse
* @throws IOException
* If there was an exception getting the entity's data.
*/
public static List <NameValuePair> parse (
final HttpEntity entity) throws IOException {
List <NameValuePair> result = Collections.emptyList();
String contentType = null;
String charset = null;
Header h = entity.getContentType();
if (h != null) {
HeaderElement[] elems = h.getElements();
if (elems.length > 0) {
HeaderElement elem = elems[0];
contentType = elem.getName();
NameValuePair param = elem.getParameterByName("charset");
if (param != null) {
charset = param.getValue();
}
}
}
if (contentType != null && contentType.equalsIgnoreCase(CONTENT_TYPE)) {
final String content = EntityUtils.toString(entity, HTTP.ASCII);
if (content != null && content.length() > 0) {
result = new ArrayList <NameValuePair>();
parse(result, new Scanner(content), charset);
}
}
return result;
}
/**
* Returns true if the entity's Content-Type header is
* <code>application/x-www-form-urlencoded.
*/
public static boolean isEncoded (final HttpEntity entity) {
Header h = entity.getContentType();
if (h != null) {
HeaderElement[] elems = h.getElements();
if (elems.length > 0) {
String contentType = elems[0].getName();
return contentType.equalsIgnoreCase(CONTENT_TYPE);
} else {
return false;
}
} else {
return false;
}
}
/**
* Adds all parameters within the Scanner to the list of
* <code>parameters, as encoded by
Other Java examples (source code examples)Here is a short list of links related to this Java URLEncodedUtils.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.