57 lines
1.8 KiB
Java
57 lines
1.8 KiB
Java
package de.unibremen.informatik.commons.net;
|
|
|
|
import java.net.URLConnection;
|
|
import java.net.URL;
|
|
import java.net.URLEncoder;
|
|
import java.util.Map;
|
|
import java.util.Iterator;
|
|
import java.io.IOException;
|
|
import java.io.File;
|
|
import java.io.InputStream;
|
|
import java.io.OutputStream;
|
|
import java.io.FileInputStream;
|
|
|
|
import de.unibremen.informatik.commons.io.IOUtils;
|
|
|
|
public class HttpPostUrlencoded {
|
|
URLConnection connection;
|
|
OutputStream os = null;
|
|
|
|
protected void write(String s) throws IOException {
|
|
os.write(s.getBytes());
|
|
}
|
|
|
|
public HttpPostUrlencoded(URL url) throws IOException {
|
|
connection = url.openConnection();
|
|
connection.setDoOutput(true);
|
|
connection.setRequestProperty("Content-Type",
|
|
"application/x-www-form-urlencoded");
|
|
os = connection.getOutputStream();
|
|
}
|
|
|
|
public InputStream post(Map parameters) throws IOException {
|
|
String name;
|
|
Object object;
|
|
if (parameters != null) {
|
|
for (Iterator i = parameters.entrySet().iterator(); i.hasNext();) {
|
|
Map.Entry entry = (Map.Entry)i.next();
|
|
name = entry.getKey().toString();
|
|
object = entry.getValue();
|
|
|
|
write(name + "=");
|
|
if (object instanceof File) {
|
|
write(URLEncoder.encode(IOUtils.getString(new FileInputStream((File)object)), "UTF-8"));
|
|
} else {
|
|
write(object.toString());
|
|
}
|
|
write("&");
|
|
}
|
|
}
|
|
os.close();
|
|
return connection.getInputStream();
|
|
}
|
|
|
|
public static InputStream post(URL url, Map parameters) throws IOException {
|
|
return new HttpPostUrlencoded(url).post(parameters);
|
|
}
|
|
}
|