网速检测
This commit is contained in:
parent
a6ae1bcda9
commit
85fdf7dbcb
@ -1,4 +1,5 @@
|
||||
package com.lovenav.utils;
|
||||
import java.io.BufferedReader;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
@ -6,6 +7,7 @@ import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.*;
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
|
||||
@ -33,6 +35,10 @@ public class UrlCheckUtil {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断链接是否有效
|
||||
* 输入链接
|
||||
*/
|
||||
public static String checkUrlConnection(String url) {
|
||||
// 创建http POST请求
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
@ -60,6 +66,9 @@ public class UrlCheckUtil {
|
||||
return String.valueOf(statusCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查http连接
|
||||
*/
|
||||
public static boolean CheckHttp(String address) throws URISyntaxException, MalformedURLException {
|
||||
URL url = new URL(address);
|
||||
URI uri = url.toURI();
|
||||
@ -70,4 +79,46 @@ public class UrlCheckUtil {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* 发送GET请求
|
||||
*/
|
||||
public static String sendGetRequest(String url) throws IOException {
|
||||
URL requestUrl = new URL(url);
|
||||
HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection();
|
||||
connection.setRequestMethod("GET");
|
||||
|
||||
int responseCode = connection.getResponseCode();
|
||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
StringBuilder response = new StringBuilder();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
response.append(line);
|
||||
}
|
||||
reader.close();
|
||||
return response.toString();
|
||||
} else {
|
||||
throw new RuntimeException("HTTP GET request failed with response code: " + responseCode);
|
||||
}
|
||||
}
|
||||
|
||||
public static void url_speed(String url) {
|
||||
String url_check = url;
|
||||
int numRequests = 10;
|
||||
long totalResponseTime = 0;
|
||||
|
||||
for (int i = 0; i < numRequests; i++) {
|
||||
long startTime = System.currentTimeMillis();
|
||||
try {
|
||||
UrlCheckUtil.sendGetRequest(url_check);
|
||||
long endTime = System.currentTimeMillis();
|
||||
long responseTime = endTime - startTime;
|
||||
totalResponseTime += responseTime;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
double averageResponseTime = (double) totalResponseTime / (1 * numRequests);
|
||||
System.out.println("Average response time: " + averageResponseTime + " ms");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user