public static boolean sendGetRequest(String path, Map<String, String> params, String enc) throws Exception{
StringBuilder sb = new StringBuilder(path);
sb.append('?');
// ?method=save&title=435435435&timelength=89&
//map迭代
for(Map.Entry<String, String> entry : params.entrySet()){
sb.append(entry.getKey()).append('=')
.append(URLEncoder.encode(entry.getValue(), enc)).append('&');
}
//去掉最后一个&
sb.deleteCharAt(sb.length()-1);
URL url = new URL(sb.toString());
//打开连接
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置请求方式
conn.setRequestMethod("GET");
//设置延迟时间
conn.setConnectTimeout(5 * 1000);
if(conn.getResponseCode()==200){
return true;
}
return false;
}
发送GET请求
最新推荐文章于 2023-10-13 09:19:46 发布