1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| public static String Post(String httpUrl, String data) { PrintWriter out = null; InputStream readStream = null; String output = data; String result = ""; try { output = "value1=" + URLEncoder.encode(output, "UTF-8"); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } try { URL postUrl = new URL(httpUrl); HttpURLConnection conn = (HttpURLConnection) postUrl.openConnection(); conn.setConnectTimeout(5000); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Charset", "UTF-8"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); out = new PrintWriter(conn.getOutputStream()); out.print(output); out.flush(); out.close(); if (conn.getResponseCode() != 200) { throw new Exception("请求url失败"); } readStream = conn.getInputStream(); result = ll.InputStreamToString(readStream); return result; } catch (Exception e) { e.printStackTrace(); result = ll.InputStreamToString(readStream); return result; } finally { if (out != null) { out.close(); } if (readStream != null) { try { readStream.close(); } catch (IOException e) { return null; } } } }
|