byte[] myData = stringBuilder.toString().getBytes();
// 设置请求体的类型是文本类型,表示当前提交的是文本数据
urlConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
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 (IOException e) {
e.printStackTrace();
}
}
return "";
}
/**
* 将一个输入流转换成指定编码的字符串
*
* @param inputStream
* @param encode
* @return
*/
private static String changeInputStream(InputStream inputStream,
String encode) {
// 内存流