java使用HttpURLConnection发送Post数据
public static String sendPost(String urlStr, String dataStr) { String result = ""; try { // 创建url资源 URL url = new URL(urlStr); // 建立http连接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 设置允许输入输出 conn.setDoOutput(true); conn.setDoInput(true); // 设置不用缓存 conn.setUseCaches(false); // 设置传递方式 conn.setRequestMethod("POST"); // 设置维持长连接 conn.setRequestProperty("Connection", "Keep-Alive"); // 设置文件字符集: conn.setRequestProperty("Charset", "UTF-8"); //转换为字节数组 byte[] data = dataStr.getBytes("UTF-8"); // 设置文件长度 conn.setRequestProperty("Content-Length", String.valueOf(data.length)); // 设置文件类型: conn.setRequestProperty("Content-Type", "text/xml");// 开始连接请求 conn.connect(); OutputStream out = conn.getOutputStream(); // 写入请求的字符串 out.write(data); out.flush(); out.close(); System.out.println(conn.getResponseCode()); // 请求返回的状态 if (conn.getResponseCode() == 200) { System.out.println("连接成功"); // 请求返回的数据 InputStream in = conn.getInputStream();try { byte[] data1 = new byte[in.available()]; in.read(data1); result = new String(data1); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else { System.out.println("no++"); } } catch (Exception e) { } return result; }