public static String sendPost(String url,String paramStr) {
// 作为StringBuffer初始化的字符串
String encode = “utf-8”;
StringBuffer buffer = new StringBuffer();
try {
if(null != paramStr && !"".equals(paramStr.trim())){
buffer.append(paramStr);
}
HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection();
urlConnection.setConnectTimeout(3000);
urlConnection.setRequestMethod(“POST”);
urlConnection.setDoInput(true);// 表示从服务器获取数据
urlConnection.setDoOutput(true);// 表示向服务器写数据
// 获得上传信息的字节大小以及长度
byte[] mydata = buffer.toString().getBytes(“utf-8”);//请求参数设置utf-8编码,防止变成客服消息
// 表示设置请求体的类型是文本类型
urlConnection.setRequestProperty(“Content-Type”,
“application/x-www-form-urlencoded;charset=utf-8”);
urlConnection.setRequestProperty(“Content-Length”,
String.valueOf(mydata.length));
// 获得输出流,向服务器输出数据
OutputStream outputStream = urlConnection.getOutputStream();
outputStream.write(mydata, 0, mydata.length);
outputStream.close();
// 获得服务器响应的结果和状态码
int responseCode = urlConnection.getResponseCode();
if (responseCode == 200) {
return changeInputStream(urlConnection.getInputStream(), encode);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return “”;
}