post方法

来源:互联网 发布:mysql 关闭数据库 编辑:程序博客网 时间:2024/05/22 00:24
public static String doPost(String urlString, String param) {
URL url = null;
HttpURLConnection URLConn = null;
OutputStreamWriter out = null;
BufferedReader reader = null;
StringBuffer resultBuf = new StringBuffer();
try {
url = new URL(urlString);
URLConn = (HttpURLConnection) url.openConnection();
URLConn.setConnectTimeout(40000);
URLConn.setReadTimeout(40000);
URLConn.setDoInput(true);
URLConn.setDoOutput(true);
URLConn.setRequestMethod("POST");
URLConn.setUseCaches(false);
URLConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
URLConn.connect();


out = new OutputStreamWriter(URLConn.getOutputStream());
out.write(param.toString());
out.flush();

reader = new BufferedReader(new InputStreamReader(URLConn.getInputStream(), "UTF-8"));
String temp = null;
while ((temp = reader.readLine()) != null) {
resultBuf.append(temp);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
if (out != null) {
out.close();
}
if (URLConn != null) {
URLConn.disconnect();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return resultBuf.toString();
}
0 0