public static String doGet(String httpUrl) {
HttpURLConnection connection = null;
InputStream inputStream = null;
BufferedReader bufferedReader = null;
String result = null;// 返回的result
try {
URL url = new URL(httpUrl);
// 通过远程url链接对象打开一个链接,强化转成httpUrlConnection类
connection = (HttpURLConnection) url.openConnection();
// 设置链接方式
connection.setRequestMethod("GET");
// 设置远程链接读取返回毫秒数
connection.setReadTimeout(15000);
// 链接服务器超时时间
connection.setConnectTimeout(6000);
// 发起请求
connection.connect();
// 通过connection链接,获取输入流
if (connection.getResponseCode() == 200) {
inputStream = connection.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
//存放数据
StringBuffer sdf = new StringBuffer();
String temp = null;
while ((temp = bufferedReader.readLine()) != null) {
sdf.append(temp);
sdf.append("\r\n");
}
result = sdf.toString();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭资源
if (null != bufferedReader) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//关闭链接
connection.disconnect();
}
return result;
}
public static void doPost(String httpUrl, String param) {
HttpURLConnection connection = null;
InputStream is = null;
OutputStream os = null;
BufferedReader bufferReader = null;
String result = null;
try {
URL url = new URL(httpUrl);
// 通过远程url连接对象打开连接
connection = (HttpURLConnection) url.openConnection();
// 设置请求方式
connection.setRequestMethod("POST");
// 设置链接服务器超时的时间
connection.setConnectTimeout(15000);
// 设置读取服务器返回数据超时时间
connection.setReadTimeout(60000);
// 默认值为:false,向远程服务器传送数/写入数据时,设置为true;
connection.setDoInput(true);
// 设置传入参数的格式:Content-type application/x-www-form-urlencoded
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// 设置鉴权信息
// connection.setRequestProperty("Authorization",
// "Bearerda3efcbf-0845-4fe3-8aba-ee040be542c0");
// 通过链接对象获取一个输出流
os = connection.getOutputStream();
// 通过输出流将对象写出去/传输出入,它是通过字节数组写出的。
os.write(param.getBytes());
// 通过链接对象获取一个输入流,向远程读取
if (connection.getResponseCode() == 200) {
is = connection.getInputStream();
// 对输入流对象进行包装:charset根据项目要求设置
bufferReader = new BufferedReader(new InputStreamReader(is, "utf-8"));
StringBuffer sbf = new StringBuffer();
String temp = null;
while ((temp = bufferReader.readLine()) != null) {
sbf.append(temp);
sbf.append("\n\t");
}
result = sbf.toString();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null == bufferReader) {
try {
bufferReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null == os) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
doGet("http://ip地址:8080/项目名/service/repairTraceService.jsp?action=getRepairReport&data=2&id=8612c7486c034e1eb1ab8c89c9089ecd&companyId=undefined&r=0.1463507952826204");
}