标签:writer 协议 exce mss line system cep uil 编码
json对象-json字符串-编码byte[] -传输-解码为字符串-对象
client:
String wholeUrl = String.format("%s?sdkappid=%d&random=%d", URL, SDKAPPID, rnd); java.net.URL object = new URL(wholeUrl); HttpURLConnection con = (HttpURLConnection) object.openConnection(); con.setDoOutput(true); con.setDoInput(true); con.setRequestProperty("Content-Type", "application/json"); con.setRequestProperty("Accept", "application/json"); con.setRequestMethod("POST"); JSONObject data = new JSONObject(); JSONObject tel = new JSONObject(); tel.put("nationcode", nationCode); String phone = phoneNumber; tel.put("phone", phone); data.put("type", "0"); data.put("msg", content); String sig = stringMD5(APPKEY.concat(phone)); data.put("sig", sig); data.put("tel", tel); OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream(), "utf-8"); wr.write(data.toString()); wr.flush(); // 显示 POST 请求返回的内容 StringBuilder sb = new StringBuilder(); int HttpResult = con.getResponseCode(); if (HttpResult == HttpURLConnection.HTTP_OK) { BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8")); String line; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); JSONObject reObj = new JSONObject(sb.toString()); String res = (String)reObj.get("result"); if(!"0".equals(res)) { System.out.println("" + sb.toString()); LOGGER.error("SmsSender::sendMsg" + "-" + phoneNumber + ","+(String)reObj.get("errmsg")); } else { } } else { LOGGER.error("send failed and rps is: " + con.getResponseMessage()); throw new SmsException("send failed and rps is: " + con.getResponseMessage()); }
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream(), "utf-8");
wr.write(data.toString());
while ((line = br.readLine()) != null)
{
sb.append(line + "\n");
}
br.close();
JSONObject reObj = new JSONObject(sb.toString());
server:
标签:writer 协议 exce mss line system cep uil 编码
原文地址:https://www.cnblogs.com/silyvin/p/10172445.html