public static String sendPost(String requrl,String param){ URL url; String sTotalString=""; try { url = new URL(requrl); URLConnection connection = url.openConnection(); connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "text/xml"); // connection.setRequestProperty("Content-Length", body.getBytes().length+""); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"); connection.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8"); out.write(param); // 向页面传递数据。post的关键所在! out.flush(); out.close(); // 一旦发送成功,用以下方法就可以得到服务器的回应: String sCurrentLine; sCurrentLine = ""; sTotalString = ""; InputStream l_urlStream; l_urlStream = connection.getInputStream(); // 传说中的三层包装阿! BufferedReader l_reader = new BufferedReader(new InputStreamReader( l_urlStream)); while ((sCurrentLine = l_reader.readLine()) != null) { sTotalString += sCurrentLine + "\r\n"; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(sTotalString); return sTotalString; }
使用上述方法
String params="{'aaa':'09'}";
String url="请求地址";
httpUtil.sendPost(url, params);
httpUtil为你的工具类对象