Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.apache.http.NameValuePair;
import java.util.List;

public class Main {
    public static String getFormDataContent(final List<? extends NameValuePair> parameters, final String boundary)
            throws IllegalArgumentException {

        if (parameters == null || parameters.size() == 0)
            throw new IllegalArgumentException("parameters error");
        final StringBuilder result = new StringBuilder();
        for (final NameValuePair parameter : parameters) {

            result.append(String.format("\r\n--%1$s\r\nContent-Disposition: form-data; name=\"%2$s\"\r\n\r\n%3$s",
                    boundary, parameter.getName(), parameter.getValue()));
        }
        return result.toString();
    }

    public static String getFormDataContent(String contentType, List<? extends NameValuePair> parameters,
            String boundary) {
        if (parameters == null || parameters.size() == 0)
            throw new IllegalArgumentException("parameters error");
        final StringBuilder result = new StringBuilder();
        for (final NameValuePair parameter : parameters) {

            result.append(String.format(
                    "\r\n--%1$s\r\nContent-Disposition: form-data; name=\"%2$s\"\r\n\r\n%3$s\r\n\r\n%4$s", boundary,
                    parameter.getName(), parameter.getValue(), boundary));
        }
        return result.toString();
    }
}