• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
helong
博客园    首页    新随笔    联系   管理    订阅  订阅

HttpURLConnection向客户端发送JSON格式的数据

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();

}

}
posted @ 2012-02-09 16:20  helong  阅读(4725)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3