private static String hcc = HttpMethodParams.HTTP_CONTENT_CHARSET;//指的是html返回的response的body的字符集
- // HTTP_ELEMENT_CHARSET:是指的是,发送http的request时候的charset
* http传输获得返回结果
*
* @param url
* @param content
* @return
*/
public static String ConnToDMS(String url, String content) {
HttpClient client = new HttpClient();
client.getParams().setParameter(hcc, "utf-8");
PostMethod method = new PostMethod(url); //post请求
method.addParameter("content", content);//情求参数
StringBuffer stringBuffer = new StringBuffer();// 对象的初始化
try {
client.executeMethod(method);// 发送请求
InputStream inputStream = method.getResponseBodyAsStream();//获取请求返回的json
InputStreamReader isr = new InputStreamReader(inputStream);//字符输入流
BufferedReader br = new BufferedReader(isr);
String str = "";
while ((str = br.readLine()) != null) {
stringBuffer.append(str);该方法的作用是追加内容到当前StringBuffer对象的末尾,类似于字符串的连接。
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 使用完成后要释放链接
method.releaseConnection();
return stringBuffer.toString();
}