com.aliyun.datahub.client.http.common.HttpForm Maven / Gradle / Ivy
The newest version!
package com.aliyun.datahub.client.http.common;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class HttpForm {
private Map parameters = new LinkedHashMap<>();
public HttpForm addParam(String name, Object value) {
if (value != null) {
parameters.put(name, String.valueOf(value));
}
return this;
}
public String toFormStr() {
StringBuilder sb = new StringBuilder();
Iterator> iterator = parameters.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = iterator.next();
try {
String encodeKey = URLEncoder.encode(entry.getKey(), "UTF-8");
String encodeValue = URLEncoder.encode(entry.getValue(), "UTF-8");
sb.append(encodeKey).append("=").append(encodeValue);
} catch (Exception e) {
e.printStackTrace();
}
if (iterator.hasNext()) {
sb.append("&");
}
}
return sb.toString();
}
}