java http请求接口返回值乱码
返回值编码格式设置为utf-8
public static String load(JSONObject query,String type,String methodName) throws Exception
{
String url = "http://localhost:"+port+adminPath+"/management/interfaceController/"+methodName;
URL restURL = new URL(url);
/*
* 此处的urlConnection对象实际上是根据URL的请求协议(此处是http)生成的URLConnection类 的子类HttpURLConnection
*/
HttpURLConnection conn = (HttpURLConnection) restURL.openConnection();
//请求方式
conn.setRequestMethod(type);
//设置是否从httpUrlConnection读入,默认情况下是true; httpUrlConnection.setDoInput(true);
conn.setDoOutput(true);
//allowUserInteraction 如果为 true,则在允许用户交互(例如弹出一个验证对话框)的上下文中对此 URL 进行检查。
conn.setAllowUserInteraction(false);
PrintStream ps = new PrintStream(conn.getOutputStream());
// username=admin&password=admin&mobileLogin=true
String param = "";
if(query != null) {
Set<String> set = query.keySet();
for (String key : set) {
String val = query.getString(key);
param += key+"="+val+"&";
}
}
if(!StringUtils.isEmpty(param)) {
param.substring(0, param.length()-1);
}
ps.print(param);
ps.close();
BufferedReader bReader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
String line,resultStr="";
while(null != (line=bReader.readLine()))
{
resultStr +=line;
}
bReader.close();
return resultStr;
}