get post2form post2json
pom.xml
<!-- httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
package com.marks.zweb.util.http;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HttpUtils {
private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);
/**
* get
*
* @param url
* @param authorization
* @return
*/
public static String get(String url, String token) {
Map<String, String> map = new HashMap<String, String>();
map.put("Authorization", token);
return get(url, map);
}
/**
* get
*
* @param url
* @param token
* @return
*/
public static String get(String url, Map<String, String> headerMap) {
if (url == null) {
return "";
}
String res = "";
Integer code = null; // 状态码
try {
HttpClient httpclient = new HttpClient();
GetMethod get = new GetMethod(url);
if (headerMap != null && headerMap.size() > 0) {
Set<String> keys = headerMap.keySet();
Iterator<String> it = keys.iterator();
while (it.hasNext()) {
String key = it.next();
get.addRequestHeader(key, headerMap.get(key));
}
}
code = httpclient.executeMethod(get);
// 返回结果
res = new String(get.getResponseBody(), "utf-8");
} catch (Exception e) {
log.error("http请求<get>失败", e);
} finally {
StringBuffer sb = new StringBuffer();
sb.append("\n");
sb.append("http请求<get>\n");
sb.append("url:" + url + "\n");
sb.append("headerMap:" + headerMap + "\n");
sb.append("---------------------\n");
sb.append("resCode:" + code + "\n");
sb.append("resBody:" + res + "\n");
sb.append("<---------------------\n");
log.info(sb.toString());
}
log.info("code:" + code);
return res;
}
/**
* post
*
* @param url
* @param bodyContent
* @return
* @throws Exception
*/
public static String post2json(String url, String token, String bodyContent) {
Map<String, String> map = new HashMap<String, String>();
map.put("Authorization", token);
return post2json(url, map, bodyContent);
}
/**
* post
*
* @param url
* @param bodyContent
* @return
* @throws Exception
*/
public static String post2json(String url, Map<String, String> headerMap, String bodyContent) {
if (url == null) {
return "";
}
String res = "";
Integer code = null; // 状态码
try {
HttpClient httpclient = new HttpClient();
PostMethod post = new PostMethod(url);
post.addRequestHeader("Content-Type", "application/json;charset=utf-8");//
post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
if (headerMap != null && headerMap.size() > 0) {
Set<String> keys = headerMap.keySet();
Iterator<String> it = keys.iterator();
while (it.hasNext()) {
String key = it.next();
post.addRequestHeader(key, headerMap.get(key));
}
}
StringRequestEntity re = new StringRequestEntity(bodyContent, "application/json", "utf-8");
post.setRequestEntity(re);
code = httpclient.executeMethod(post);
// 返回结果
res = new String(post.getResponseBody(), "utf-8");
} catch (Exception e) {
log.error("http请求<post>失败", e);
} finally {
StringBuffer sb = new StringBuffer();
sb.append("\n");
sb.append("http请求<post>\n");
sb.append("url:" + url + "\n");
sb.append("headerMap:" + headerMap + "\n");
sb.append("reqBody:" + bodyContent + "\n");
sb.append("---------------------\n");
sb.append("resCode:" + code + "\n");
sb.append("resBody:" + res + "\n");
sb.append("<---------------------\n");
log.info(sb.toString());
}
log.info("code:" + code);
return res;
}
/**
* post
*
* @param url
* @param bodyContent
* @return
* @throws Exception
*/
public static String post2Form(String url, String token, Map<String, String> argsMap) {
Map<String, String> map = new HashMap<String, String>();
map.put("Authorization", token);
return post2Form(url, map, argsMap);
}
public static String post2Form(String url, Map<String, String> headerMap, Map<String, String> argsMap) {
if (url == null) {
return "";
}
String res = "";
Integer code = null; // 状态码
try {
HttpClient httpclient = new HttpClient();
PostMethod post = new PostMethod(url);
post.addRequestHeader("Content-Type", "multipart/form-data;charset=utf-8");//
post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
if (headerMap != null && headerMap.size() > 0) {
Set<String> keys = headerMap.keySet();
Iterator<String> it = keys.iterator();
while (it.hasNext()) {
String key = it.next();
post.addRequestHeader(key, headerMap.get(key));
}
}
if (argsMap != null && argsMap.size() > 0) {
NameValuePair[] data = new NameValuePair[argsMap.size()];
int index = 0;
Set<String> keys = argsMap.keySet();
Iterator<String> it = keys.iterator();
while (it.hasNext()) {
String key = it.next();
data[index] = new NameValuePair(key, argsMap.get(key));
index++;
}
post.setRequestBody(data);
}
code = httpclient.executeMethod(post);
// 返回结果
res = new String(post.getResponseBody(), "utf-8");
} catch (Exception e) {
log.error("http请求<post>失败", e);
} finally {
StringBuffer sb = new StringBuffer();
sb.append("\n");
sb.append("http请求<post>\n");
sb.append("url:" + url + "\n");
sb.append("headerMap:" + headerMap + "\n");
sb.append("argsMap:" + argsMap + "\n");
sb.append("---------------------\n");
sb.append("resCode:" + code + "\n");
sb.append("resBody:" + res + "\n");
sb.append("<---------------------\n");
log.info(sb.toString());
}
log.info("code:" + code);
return res;
}
public static void main(String[] args) {
String token = "1.XeyJzdWIiOjIyMCwiaHZtIjowLCJhdmF0YXIiOiJodHRwOlwvXC90aGlyZHd4LnFsb2dvLmNuXC9tbW9wZW5cL3ZpXzMyXC9HQmZTYzBwY0MyaWNJZ05yNDc0TWlhdEdsaWFlWkpoUG15anQ5QmliZkJnRFJBV29ZQ1JQbDU1emdzdFk1c1oza0ZCRFdVYW50dlo4NmZWYjNUQWtGSDZ5V3dcLzEzMiIsIm5hbWUiOiJcdTUxN2JcdTUzZDFcdTAwYWVcdTY3MDlcdTU5NDdcdTY1YjlcdTMwMTBcdTViOThcdTY1YjlcdThiYTRcdThiYzFcdTMwMTEiLCJsb2dpbl90YWciOiJNVV8ld0YtIXY0IiwicmVmcmVzaF90aW1lIjoxNTc1ODc3MTcwLCJqdGkiOiJkZHZ6RmczSjl5cHRSOERrIiwibmJmIjoxNTc1ODczNTcwLCJleHAiOjE1NzY0NzgzNzAsImlhdCI6MTU3NTg3MzU3MCwiaXNzIjoiaHR0cHM6XC9cL3Rlc3QtYXBwMi5mZWpqLmNvbVwvYXV0aG9yaXphdGlvbnNcL3RoaXJkIn0.VGajNaDjhtW1HYM-7TXHXTDkoQeDbiof9kn_y5hZ1x4";
String sysToken ="Bearer EMk0Ep4iOikjVaPiLdkhbGCiOikHUNHazik9.EMkYDGKiOik3D3lsLWfKbWluHiwi1pzNHJoiD3D5bdahZGaXbiaNZpk21WzlHiwi1Wf0HJoxzTCazcP5ycg3LdklEI4iOJeaOcyMyJUwOcCsHnzaqiH6HJeiLdkNZpzN1W9uHJX-HnVNZpkuqWalHJoiqWRv1WAiLdkMZWfsbmfvZSH6Hui2hEE6X8EuoEEPhuWRmdk9LdkMb2xlC0lKHJoiySk9.TqUYTmnmOIHljlOFdsmMk6nf7OBUNUK8ZHCpmdW5hne";
String res="";
String url = "https://test-app.fejj.com/api/act/userPackage/goodsList";
res = get(url, token);
url = "https://test-app.fejjX.com/api/act/exchange/goods/batchUpdateAuditStatus";
url = "http://localhost:8080/api/act/exchange/goods/batchUpdateAuditStatus";
Map<String, String> argsMap =new HashMap<String, String>();
argsMap.put("agreeIds", "0,1");
argsMap.put("refuseIds", "0,2,3,4");
argsMap.put("sysToken", sysToken);
res = post2Form(url, token, argsMap);
System.out.println(res);
}
}