public class CommonUtils {
public static String DOU_YIN_BASE_URL = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=";
public static String HUO_SHAN_BASE_URL = " https://share.huoshan.com/api/item/info?item_id=";
public static String DOU_YIN_DOMAIN = "douyin";
public static String HUO_SHAN_DOMAIN = "huoshan";
public static String getLocation(String url) {
try {
URL serverUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) serverUrl.openConnection();
conn.setRequestMethod("GET");
conn.setInstanceFollowRedirects(false);
conn.setRequestProperty("User-agent", "ua");//模拟手机连接
conn.connect();
String location = conn.getHeaderField("Location");
return location;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static String matchNo(String redirectUrl) {
List<String> results = new ArrayList<>();
Pattern p = Pattern.compile("video/([\\w/\\.]*)/");
Matcher m = p.matcher(redirectUrl);
while (!m.hitEnd() && m.find()) {
results.add(m.group(1));
}
return results.get(0);
}
public static String hSMatchNo(String redirectUrl) {
List<String> results = new ArrayList<>();
Pattern p = Pattern.compile("item_id=([\\w/\\.]*)&");
Matcher m = p.matcher(redirectUrl);
while (!m.hitEnd() && m.find()) {
results.add(m.group(1));
}
return results.get(0);
}
public static String httpGet2(String urlStr) throws Exception {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "text/json;charset=utf-8");
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
StringBuffer buf = new StringBuffer();
String inputLine = in.readLine();
while (inputLine != null) {
buf.append(inputLine).append("\r\n");
inputLine = in.readLine();
}
in.close();
return buf.toString();
}
/**
* 使用Get方式获取数据
*
* @param url URL包括参数,http://HOST/XX?XX=XX&XXX=XXX
* @return
*/
public static String httpGet(String url) {
String result = "";
BufferedReader in = null;
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
public static String parseUrl(String url) {
String host = "";
Pattern p = Pattern.compile("http[:|/|\\w|\\.]+");
Matcher matcher = p.matcher(url);
if (matcher.find()) {
host = matcher.group();
}
return host.trim();
}
/**
* 查找域名(以 https开头 com结尾)
*
* @param url
* @return
*/
public static String getDomainName(String url) {
String host = "";
Pattern p = Pattern.compile("https://.*\\.com");
Matcher matcher = p.matcher(url);
if (matcher.find()) {
host = matcher.group();
}
return host.trim();
}
}
|