io.leopard.httpnb.HttpnbRequest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leopard-boot-httpnb Show documentation
Show all versions of leopard-boot-httpnb Show documentation
Http操作类库。支持Get、Post、文件上传等常用操作。可在TopNB查看耗时统计信息。
The newest version!
package io.leopard.httpnb;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
public class HttpnbRequest {
private String cookies;
private List paramList = new ArrayList();
List> fileList = new ArrayList>();
public void addParamater(String name, Object value) {
Param param = new Param();
param.setName(name);
param.setValue(value);
this.paramList.add(param);
}
public String getCookies() {
return cookies;
}
public void setCookies(String cookies) {
this.cookies = cookies;
}
public void addFile(String name, String path) {
Entry fileEntry = new SimpleEntry(name, path);
fileList.add(fileEntry);
}
public String execute(String url) {
HttpHeader header = new HttpHeaderPostImpl(-1);
if (cookies != null) {
header.setCookie(cookies);
}
try {
if (fileList.isEmpty()) {
return this.doPost(url, header);
}
else {
return this.doUpload(url, header);
}
}
catch (IOException e) {
throw new HttpException(e, header);
}
}
protected String doPost(String url, HttpHeader header) throws IOException {
for (Param param : paramList) {
// System.out.println("addParam:" + param);
header.addParam(param);
}
HttpURLConnection conn = header.openConnection(url);
return Httpnb.execute(conn, null);
}
protected String doUpload(String url, HttpHeader header) throws IOException {
HttpURLConnection conn = header.openConnection(url);
HttpUpload.upload(conn, fileList, paramList);
return Httpnb.execute(conn, null);
}
}