1 /*
2 * ====================================================================
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 * ====================================================================
20 *
21 * This software consists of voluntary contributions made by many
22 * individuals on behalf of the Apache Software Foundation. For more
23 * information on the Apache Software Foundation, please see
24 * <http://www.apache.org/>.
25 *
26 */
27 package org.apache.http.client.entity;
28
29 import java.io.UnsupportedEncodingException;
30 import java.nio.charset.Charset;
31 import java.util.List;
32
33 import org.apache.http.NameValuePair;
34 import org.apache.http.client.utils.URLEncodedUtils;
35 import org.apache.http.entity.ContentType;
36 import org.apache.http.entity.StringEntity;
37 import org.apache.http.protocol.HTTP;
38
39 /**
40 * An entity composed of a list of url-encoded pairs.
41 * This is typically useful while sending an HTTP POST request.
42 *
43 * @since 4.0
44 */
45 public class UrlEncodedFormEntity extends StringEntity {
46
47 /**
48 * Constructs a new {@link UrlEncodedFormEntity} with the list
49 * of parameters in the specified encoding.
50 *
51 * @param parameters list of name/value pairs
52 * @param charset encoding the name/value pairs be encoded with
53 * @throws UnsupportedEncodingException if the encoding isn't supported
54 */
55 public UrlEncodedFormEntity (
56 final List <? extends NameValuePair> parameters,
57 final String charset) throws UnsupportedEncodingException {
58 super(URLEncodedUtils.format(parameters,
59 charset != null ? charset : HTTP.DEF_CONTENT_CHARSET.name()),
60 ContentType.create(URLEncodedUtils.CONTENT_TYPE, charset));
61 }
62
63 /**
64 * Constructs a new {@link UrlEncodedFormEntity} with the list
65 * of parameters in the specified encoding.
66 *
67 * @param parameters iterable collection of name/value pairs
68 * @param charset encoding the name/value pairs be encoded with
69 *
70 * @since 4.2
71 */
72 public UrlEncodedFormEntity (
73 final Iterable <? extends NameValuePair> parameters,
74 final Charset charset) {
75 super(URLEncodedUtils.format(parameters,
76 charset != null ? charset : HTTP.DEF_CONTENT_CHARSET),
77 ContentType.create(URLEncodedUtils.CONTENT_TYPE, charset));
78 }
79
80 /**
81 * Constructs a new {@link UrlEncodedFormEntity} with the list
82 * of parameters with the default encoding of {@link HTTP#DEFAULT_CONTENT_CHARSET}
83 *
84 * @param parameters list of name/value pairs
85 * @throws UnsupportedEncodingException if the default encoding isn't supported
86 */
87 public UrlEncodedFormEntity (
88 final List <? extends NameValuePair> parameters) throws UnsupportedEncodingException {
89 this(parameters, (Charset) null);
90 }
91
92 /**
93 * Constructs a new {@link UrlEncodedFormEntity} with the list
94 * of parameters with the default encoding of {@link HTTP#DEFAULT_CONTENT_CHARSET}
95 *
96 * @param parameters iterable collection of name/value pairs
97 *
98 * @since 4.2
99 */
100 public UrlEncodedFormEntity (
101 final Iterable <? extends NameValuePair> parameters) {
102 this(parameters, null);
103 }
104
105 }