package com.longfx.transfer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.HttpStatus;
/**
* @author zhangmy
*/
public class HttpUtil {
/**
* @param path 请求地址
* @param obj 请求数据
* @param method 请求方法 GET、POST、DELETE、PUT
* @return
*/
public static String doExecute(String path, String obj, String method) {
try {
//创建连接
URL url = new URL(path);
HttpURLConnection connection;
StringBuffer sbuffer = null;
//添加 请求内容
connection = (HttpURLConnection) url.openConnection();
//设置http连接属性
// http正文内,因此需要设为true, 默认情况下是false;
connection.setDoOutput(true);
// 设置是否从httpUrlConnection读入,默认情况下是true;
connection.setDoInput(true);
// 可以根据需要 提交 GET、POST、DELETE、PUT等http提供的功能
connection.setRequestMethod(method);
//connection.setUseCaches(false);//设置缓存,注意设置请求方法为post不能用缓存
// connection.setInstanceFollowRedirects(true);
//设置请 求的服务器网址,域名,例如***.**.***.***
//connection.setRequestProperty("Host", "*******");
//设定 请求格式 json,也可以设定xml格式的
//connection.setRequestProperty("Content-Type", " application/json");
connection.setRequestProperty("Content-Type", " application/x-www-form-urlencoded");
//设置编码语言
connection.setRequestProperty("Accept-Charset", "utf-8");
// 设置请求的token
//connection.setRequestProperty("X-Auth-Token", "token");
//设置连接的状态
connection.setRequestProperty("Connection", "keep-alive");
//设置传输编码
connection.setRequestProperty("Transfer-Encoding", "chunked");
//设置文件请求的长度
connection.setRequestProperty("Content-Length", obj.toString().getBytes().length + "");
//设置读取超时时间
connection.setReadTimeout(10000);
//设置连接超时时间
connection.setConnectTimeout(10000);
connection.connect();
//向对象输出流写出数据,这些数据将存到内存缓冲区中
OutputStream out = connection.getOutputStream();
//out.write(new String("测试数据").getBytes());
out.write(obj.toString().getBytes());
//刷新对象输出流,将任何字节都写入潜在的流中
out.flush();
// 关闭流对象,此时,不能再向对象输出流写入任何数据,先前写入的数据存在于内存缓冲区中
out.close();
//读取响应
if (connection.getResponseCode() == HttpStatus.SC_OK) {
// 从服务器获得一个输入流
//调用HttpURLConnection连接对象的getInputStream()函数, 将内存缓冲区中封装好的完整的HTTP请求电文发送到服务端。
InputStreamReader inputStream = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(inputStream);
String lines;
sbuffer = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sbuffer.append(lines);
}
reader.close();
} else {
System.err.println(connection.getResponseCode());
}
//断开连接
connection.disconnect();
return sbuffer.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
Test工具类,GET 拼接URL,可以改为POST,用JSON或Key-value形式,此处不再列举
package com.longfx.transfer;
import java.util.HashMap;
import java.util.Map;
public class TransApi {
private static final String TRANS_API_HOST = "http://127.0.0.1:8080/longfx-erp/api/ecamera/notify";
private String appid;
private String securityKey;
public TransApi(){}
public TransApi(String appid, String securityKey) {
this.appid = appid;
this.securityKey = securityKey;
}
public String getTransfer(String imgUrl,String packageOrderId){
Map<String, String> params = buildParams(imgUrl, packageOrderId);
String result = HttpUtil.doExecute(TRANS_API_HOST, buildParam(imgUrl, packageOrderId), "GET");
return result;
}
public static void main(String[] args) {
TransApi api = new TransApi();
//System.out.println(api.getTransfer("1", "111.jpg"));
String str = HttpUtil.doExecute(TRANS_API_HOST, "imgUrl=222.jpg&&packageOrderId=2", "GET");
System.out.println(str);
}
private String buildParam (String imgUrl,String packageOrderId){
return "imgUrl="+imgUrl+"&&packageOrderId="+packageOrderId;
}
//拼接Key-value形式参数
private Map<String, String> buildParams(String imgUrl,String packageOrderId) {
Map<String, String> params = new HashMap<String, String>(16);
params.put("packageOrderId", packageOrderId);
params.put("imgUrl", imgUrl);
//params.put("appid", appid);
// 随机数
//String salt = String.valueOf(System.currentTimeMillis());
//params.put("salt", salt);
// 签名 加密前的原文
//String src = appid + query + salt + securityKey;
//params.put("sign", MD5.md5(src));
return params;
}
}