Added URL encoded form entity

git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@528521 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2007-04-13 15:21:04 +00:00
parent 48c6f17ba1
commit 04d43a0dd6
2 changed files with 103 additions and 4 deletions

View File

@ -0,0 +1,97 @@
/*
* $Header$
* $Revision$
* $Date$
*
* ====================================================================
*
* 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.methods;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import org.apache.http.Header;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLUtils;
import org.apache.http.entity.AbstractHttpEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EncodingUtils;
public class UrlEncodedFormEntity extends AbstractHttpEntity {
/** The Content-Type for www-form-urlencoded. */
public static final String FORM_URL_ENCODED_CONTENT_TYPE =
"application/x-www-form-urlencoded";
private final byte[] content;
public UrlEncodedFormEntity(
final NameValuePair[] fields,
final String charset) throws UnsupportedEncodingException {
super();
String s = URLUtils.formUrlEncode(fields, charset);
this.content = EncodingUtils.getAsciiBytes(s);
}
public UrlEncodedFormEntity(final NameValuePair[] fields) {
super();
String s = URLUtils.simpleFormUrlEncode(fields, HTTP.UTF_8);
this.content = EncodingUtils.getAsciiBytes(s);
}
public boolean isRepeatable() {
return true;
}
public long getContentLength() {
return this.content.length;
}
public InputStream getContent() throws IOException {
return new ByteArrayInputStream(this.content);
}
public Header getContentType() {
return new BasicHeader(HTTP.CONTENT_TYPE, FORM_URL_ENCODED_CONTENT_TYPE);
}
public boolean isStreaming() {
return false;
}
public void writeTo(final OutputStream outstream) throws IOException {
if (outstream == null) {
throw new IllegalArgumentException("Output stream may not be null");
}
outstream.write(this.content);
outstream.flush();
}
}

View File

@ -71,12 +71,14 @@ public class URLUtils {
* *
* @since 4.0 * @since 4.0
*/ */
public static String formUrlEncode(final NameValuePair[] pairs, final String charset) { public static String simpleFormUrlEncode(
final NameValuePair[] pairs,
final String charset) {
try { try {
return doFormUrlEncode(pairs, charset); return formUrlEncode(pairs, charset);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
try { try {
return doFormUrlEncode(pairs, DEFAULT_CHARSET); return formUrlEncode(pairs, DEFAULT_CHARSET);
} catch (UnsupportedEncodingException fatal) { } catch (UnsupportedEncodingException fatal) {
// Should never happen. ISO-8859-1 must be supported on all JVMs // Should never happen. ISO-8859-1 must be supported on all JVMs
throw new Error("HttpClient requires " + DEFAULT_CHARSET + " support"); throw new Error("HttpClient requires " + DEFAULT_CHARSET + " support");
@ -104,7 +106,7 @@ public class URLUtils {
* *
* @since 2.0 final * @since 2.0 final
*/ */
private static String doFormUrlEncode( public static String formUrlEncode(
final NameValuePair[] pairs, final NameValuePair[] pairs,
final String charset) throws UnsupportedEncodingException { final String charset) throws UnsupportedEncodingException {
CharArrayBuffer buf = new CharArrayBuffer(32); CharArrayBuffer buf = new CharArrayBuffer(32);