public static String test(String paramter){
try{
URL url = new URL("http://127.0.0.1:8056/api?paramter="+paramter);
//调用URL对象的openConnection( )来获取HttpURLConnection对象实例
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//请求方法为POST
conn.setRequestMethod("POST");
//设置连接超时为5秒
conn.setConnectTimeout(5000);
//允许输入输出
conn.setDoInput(true);
conn.setDoOutput(true);
//不能缓存
conn.setUseCaches(false);
//设置头部信息
conn.setRequestProperty("headerdata", "ceshiyongde");
//设置 Content-Type
conn.setRequestProperty("Content-Type", "application/Json; charset=UTF-8");
//服务器返回东西了,先对响应码判断
String result = null;
if (conn.getResponseCode() == 200) {
//用getInputStream()方法获得服务器返回的输入流
InputStream in = conn.getInputStream();
//流转换为二进制数组,read()是转换方法
byte[] data = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = in.read(data)) != -1){
bos.write(data,0,len);
}
bos.close();
result= new String(bos.toByteArray(), "UTF-8");
in.close();
}
return result;
}catch (Exception e){
e.printStackTrace();
return null;
}
}