...
|
...
|
@@ -16,45 +16,48 @@ public class HttpTools { |
|
|
|
|
|
/**
|
|
|
* 获取当前时间,10位
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
public static String getNow(){
|
|
|
public static String getNow() {
|
|
|
Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
|
|
|
return String.valueOf(c.getTimeInMillis()/1000);
|
|
|
return String.valueOf(c.getTimeInMillis() / 1000);
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 发送http请求
|
|
|
*
|
|
|
* @param url 请求url
|
|
|
* @param type 请求类型:GET,POST,DELETE
|
|
|
* @param params 请求参数:map类型
|
|
|
* @return
|
|
|
*/
|
|
|
public static String requestByMapWithToken(String url, String type, Map<String, String> params, String authorization){
|
|
|
String reqbody = getRequestData(params,"UTF-8").toString();
|
|
|
String result = requestByString(url + "?" + reqbody,type,authorization,null);
|
|
|
public static String requestByMapWithToken(String url, String type, Map<String, String> params, String authorization) {
|
|
|
String reqbody = getRequestData(params, "UTF-8").toString();
|
|
|
String result = requestByString(url + "?" + reqbody, type, authorization, null);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
public static String requestByMap(String url, String type,Map<String, String> params){
|
|
|
return requestByMapWithToken(url,type,params,"");
|
|
|
public static String requestByMap(String url, String type, Map<String, String> params) {
|
|
|
return requestByMapWithToken(url, type, params, "");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送http请求
|
|
|
*
|
|
|
* @param url 请求url
|
|
|
* @param type 请求类型:GET,POST,DELETE
|
|
|
* @param reqbody 请求参数:String类型
|
|
|
* @return
|
|
|
*/
|
|
|
public static String requestByString(String url, String type, String authorization,String reqbody){
|
|
|
public static String requestByString(String url, String type, String authorization, String reqbody) {
|
|
|
HttpURLConnection con = null;
|
|
|
String result = null;
|
|
|
try {
|
|
|
con = getHttpConnection( url , type, authorization);
|
|
|
con = getHttpConnection(url, type, authorization);
|
|
|
//you can add any request body here if you want to post
|
|
|
if( reqbody != null){
|
|
|
if (reqbody != null) {
|
|
|
DataOutputStream out = new DataOutputStream(con.getOutputStream());
|
|
|
out.writeBytes(reqbody);
|
|
|
out.flush();
|
...
|
...
|
@@ -64,7 +67,7 @@ public class HttpTools { |
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
|
|
String temp = null;
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
while((temp = in.readLine()) != null){
|
|
|
while ((temp = in.readLine()) != null) {
|
|
|
sb.append(temp).append(" ");
|
|
|
}
|
|
|
result = sb.toString();
|
...
|
...
|
@@ -76,21 +79,21 @@ public class HttpTools { |
|
|
|
|
|
}
|
|
|
|
|
|
private static HttpURLConnection getHttpConnection(String url, String type, String authorization){
|
|
|
private static HttpURLConnection getHttpConnection(String url, String type, String authorization) {
|
|
|
URL uri = null;
|
|
|
HttpURLConnection con = null;
|
|
|
try{
|
|
|
try {
|
|
|
uri = new URL(url);
|
|
|
con = (HttpURLConnection) uri.openConnection();
|
|
|
con.setRequestMethod(type); //type: POST, PUT, DELETE, GET
|
|
|
if(type.equals("POST"))
|
|
|
if (type.equals("POST"))
|
|
|
con.setDoOutput(true);
|
|
|
con.setConnectTimeout(60000); //60 secs
|
|
|
con.setReadTimeout(60000); //60 secs
|
|
|
con.setConnectTimeout(2000); //2 secs
|
|
|
con.setReadTimeout(2000); //2 secs
|
|
|
con.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
|
|
con.setRequestProperty("authorization",authorization);
|
|
|
}catch(Exception e){
|
|
|
System.out.println( "connection i/o failed" );
|
|
|
con.setRequestProperty("authorization", authorization);
|
|
|
} catch (Exception e) {
|
|
|
System.out.println("connection i/o failed");
|
|
|
}
|
|
|
|
|
|
|
...
|
...
|
@@ -100,7 +103,7 @@ public class HttpTools { |
|
|
private static StringBuffer getRequestData(Map<String, String> params, String encode) {
|
|
|
StringBuffer stringBuffer = new StringBuffer(); //存储封装好的请求体信息
|
|
|
try {
|
|
|
for(Map.Entry<String, String> entry : params.entrySet()) {
|
|
|
for (Map.Entry<String, String> entry : params.entrySet()) {
|
|
|
stringBuffer.append(entry.getKey())
|
|
|
.append("=")
|
|
|
.append(URLEncoder.encode(entry.getValue(), encode))
|
...
|
...
|
|