package com.iapppay.statistics.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import com.iapppay.common.util.JSONUtils;
public class HttpClientTest {
/**
* @param args
* @throws InterruptedException
* @throws IOException
*/
public static void main(String[] args) throws InterruptedException, IOException {
URL url = null;
HttpURLConnection httpCon = null;
//url = new URL("http://114.119.55.158:9876/passport");
//url = new URL("http://192.168.0.232:8080/appstore-1.0/msg");
//url = new URL("http://115.174.23.226:9876/passport");
//url = new URL("http://192.168.0.125:4396/statistics");
url = new URL("http://192.168.0.180:8080/statistics");
httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("POST");
Operation op = new Operation();
op.setIMEI("LKC123456789");
op.setIMSI("13530639054");
op.setGroupNum(1);
List<Schema> schames = new ArrayList<Schema>();
op.setGroupList(schames);
Schema s1 = new Schema();
s1.setEventID(546887223);
s1.setCount(2);
s1.setWaresID("KECED000001852");
s1.setChargePoint(56875654);
s1.setEventInfo("test s1");
schames.add(s1);
Schema s2 = new Schema();
s2.setEventID(546002541);
s2.setCount(4);
s2.setWaresID("KECED56878553");
s2.setChargePoint(127025550);
s2.setEventInfo("测试 s2");
schames.add(s2);
String message = JSONUtils.toJson(op); //JSON工具类,转换对象为JSON格式
// String key = "361806691";
// String newSign = key + message + key;
// String mkSign = MD5.md5Digest(newSign);
// httpCon.addRequestProperty(HttpConst.BODY_SIGN, mkSign);
//String message ="lalallalalalalal";
OutputStream output = httpCon.getOutputStream();
output.write(message.getBytes("UTF-8"));
output.flush();
output.close();
InputStream in = httpCon.getInputStream();
byte[] readByte = new byte[1024];
// 读取返回的内容
int readCount = in.read(readByte, 0, 1024);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while (readCount != -1) {
baos.write(readByte, 0, readCount);
readCount = in.read(readByte, 0, 1024);
}
String responseBody = new String(baos.toByteArray(), "UTF-8");
System.out.println(responseBody);
baos.close();
//String rKey = httpCon.getHeaderField(HttpConst.BODY_SIGN);
//String rSign = key + responseBody + key;
//String rmkSign = MD5.md5Digest(rSign);
// if (rmkSign.equals(rKey)) {
// System.out.println("成功");
// System.out.println(responseBody);
// } else {
// System.out.println("失败");
// System.out.println(responseBody);
// }
if (httpCon != null)
httpCon.disconnect();
}
}