Java String Encode encodeDataPair(final StringBuilder buffer, final String key, final String value)

Here you can find the source of encodeDataPair(final StringBuilder buffer, final String key, final String value)

Description

Encode a key/value data pair to be used in a HTTP post request.

License

Open Source License

Parameter

Parameter Description
buffer a parameter
key a parameter
value a parameter

Declaration

private static void encodeDataPair(final StringBuilder buffer, final String key, final String value)
        throws UnsupportedEncodingException 

Method Source Code


//package com.java2s;
import java.io.*;

import java.net.URLEncoder;

public class Main {
    /**/*from  w  ww.j  a v a  2 s  . c  om*/
     * <p>Encode a key/value data pair to be used in a HTTP post request. This
     * INCLUDES a & so the first key/value pair MUST be included manually,
     * e.g:</p>
     * <code>
     * StringBuffer data = new StringBuffer();
     * data.append(encode("guid")).append('=').append(encode(guid));
     * encodeDataPair(data, "version", description.getVersion());
     * </code>
     *
     * @param buffer
     * @param key
     * @param value
     * @return
     */
    private static void encodeDataPair(final StringBuilder buffer, final String key, final String value)
            throws UnsupportedEncodingException {
        buffer.append('&').append(encode(key)).append('=').append(encode(value));
    }

    /**
     * Encode text as UTF-8
     *
     * @param text
     * @return
     */
    private static String encode(String text) throws UnsupportedEncodingException {
        return URLEncoder.encode(text, "UTF-8");
    }
}

Related

  1. encode(String text, String encoding)
  2. encode(String val, String encoding)
  3. encode(String value, String encoding)
  4. encodeBase64(String str)
  5. encodeConvenience(String str)
  6. encodeDoublePercent(String input)
  7. encodeDownloadFileName(String s)
  8. encodeFilename(final String filename, final String userAgent)
  9. encodeFilename(String filename, String encoding)