/** * 执行Post请求 * * @param url 基础url * @param query 提交的数据 * @param out 服务器回复的字符串 * @return */ private int HttpRequest(String url, String query, StringBuilder out) { URL urlobj; HttpURLConnection connection = null; try { urlobj = new URL(url); connection = (HttpURLConnection) urlobj.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Content-Length", "" + query.length()); connection.setRequestProperty("charset", "utf-8"); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); connection.setConnectTimeout(HTTP_CONNECT_TIMEOUT); connection.setReadTimeout(HTTP_READ_TIMEOUT); // Send request DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.writeBytes(query.toString()); wr.flush(); wr.close(); // Get Response InputStream is = connection.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is)); String line; while ((line = rd.readLine()) != null) { out.append(line); out.append('\r'); } rd.close(); } catch (Exception e) { e.printStackTrace(); L.i("HttpRequest Exception:" + e.getMessage()); out.append(SEND_MSG_ERROR); // 消息发送失败,返回错误,执行重发 } if (connection != null) connection.disconnect(); return 0; }
/** * 执行Post请求前数据处理,加密之类 * * @param data 请求的数据 * @return */ public String PostHttpRequest(RestApi data) { StringBuilder sb = new StringBuilder(); String channel = data.remove(RestApi._CHANNEL_ID); if (channel == null) channel = "channel"; try { // Unix时间戳:是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数 data.put(RestApi._TIMESTAMP, Long.toString(System.currentTimeMillis() / 1000)); data.remove(RestApi._SIGN); sb.append(mHttpMethod); sb.append(mUrl); sb.append(channel); for (Map.Entry<String, String> i : data.entrySet()) { sb.append(i.getKey()); sb.append('='); sb.append(i.getValue()); } sb.append(mSecretKey); // System.out.println( "PRE: " + sb.toString() ); // System.out.println( "UEC: " + URLEncoder.encode(sb.toString(), // "utf-8") ); MessageDigest md = MessageDigest.getInstance("MD5"); md.reset(); // md.update( URLEncoder.encode(sb.toString(), "utf-8").getBytes() // ); Log.e("TAG", sb.toString()); md.update(urlencode(sb.toString()).getBytes()); byte[] md5 = md.digest(); sb.setLength(0); for (byte b : md5) sb.append(String.format("%02x", b & 0xff)); data.put(RestApi._SIGN, sb.toString()); // System.out.println( "MD5: " + sb.toString()); sb.setLength(0); for (Map.Entry<String, String> i : data.entrySet()) { sb.append(i.getKey()); sb.append('='); // sb.append(i.getValue()); // sb.append(URLEncoder.encode(i.getValue(), "utf-8")); sb.append(urlencode(i.getValue())); sb.append('&'); } sb.setLength(sb.length() - 1); // System.out.println( "PST: " + sb.toString() ); // System.out.println( mUrl + "?" + sb.toString() ); } catch (Exception e) { e.printStackTrace(); L.i("PostHttpRequest Exception:" + e.getMessage()); return SEND_MSG_ERROR; // 消息发送失败,返回错误,执行重发 } StringBuilder response = new StringBuilder(); HttpRequest(mUrl + channel, sb.toString(), response); return response.toString(); }