发送get请求获取的Ping值捕捉解析
public static String post(String path) throws Exception{
HttpURLConnection httpConn=null;
BufferedReader in=null;
PrintWriter out=null;
try {
URL url=new URL(path);
httpConn=(HttpURLConnection)url.openConnection();
httpConn.setRequestMethod("GET");
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
//发送post请求参数
out=new PrintWriter(httpConn.getOutputStream());
//out.println(params);
out.flush();
//读取响应
if(httpConn.getResponseCode()==HttpURLConnection.HTTP_OK){
StringBuffer content=new StringBuffer();
String tempStr="";
in=new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
while((tempStr=in.readLine())!=null){
content.append(tempStr);
}
return content.toString();
}else{
throw new Exception("请求出现了问题!");
}
} catch (IOException e) {
e.printStackTrace();
}finally{
in.close();
out.close();
httpConn.disconnect();
}
return null;
}
public static void main(String args[]) throws Exception {
String url="www.qq.com";
//"http://103.74.174.24:88/ping.php?host="+url
String str =Test04.post("http://103.74.174.24:88/ping.php?host="+url);
System.out.println(str);
String pattern = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\."
+ "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\."
+ "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\."
+ "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";
//(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(str);
while (m.find()) {
String ip=m.group(0); //获取ip
String maxtime=m.group(1); //获取最大响应时间
String mintime=m.group(2); //获取最小响应时间
String avgtime=m.group(3); //获取bytes
}
System.out.println(m.matches());
}
问题可能在于正则表达那块,那个正则表达式是验证ip地址的,我需要将我的str中的所有东西一起放入才对,求我这个方法的解决方案。。。