package com.wf.oa;
import javax.net.ssl.*;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
public class Util
{
public static String http_dns(String host)
{
try
{
URL lll = new URL("http://119.29.29.29/d?dn="+host);
HttpURLConnection connection = (HttpURLConnection) lll.openConnection();// 打开连接
connection.setRequestMethod("GET");
connection.connect();// 连接会话
// 获取输入流
BufferedReader br= new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null)
{// 循环读取流
sb.append(line);
}
br.close();// 关闭流
connection.disconnect();// 断开连接
String hosts = sb.toString();
if(hosts.contains(";")){
return hosts.split(";")[0];
}else{
return hosts;
}
}
catch (Exception e)
{
System.out.println(e.toString());
}
return null;
}
public static String curl_with_referer(String url, String referer)
{
try
{
URL lll = new URL(url);
HttpURLConnection connection = (HttpURLConnection) lll.openConnection();// 打开连接
connection.setRequestMethod("GET");
connection.setRequestProperty("Referer", referer);
connection.connect();// 连接会话
// 获取输入流
BufferedReader br= new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null)
{// 循环读取流
sb.append(line);
}
br.close();// 关闭流
connection.disconnect();// 断开连接
return sb.toString();
}
catch (Exception e)
{
System.out.println(e.toString());
}
return null;
}
public static String get_redirected_url(String url){
String location =null;
try {
URL serverUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) serverUrl.openConnection();
connection.setRequestMethod("GET");
// 必须设置false,否则会自动redirect到Location的地址
connection.setInstanceFollowRedirects(false);
connection.addRequestProperty("Accept-Charset", "UTF-8;");
connection.addRequestProperty("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Firefox/3.6.8");
connection.connect();
location = connection.getHeaderField("Location");
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
public static String post_with_data(String url, String data)
{
try
{
URL lll = new URL(url);
HttpURLConnection connection = (HttpURLConnection) lll.openConnection();// 打开连接
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Accept", "*/*");
connection.setDoOutput(true);
connection.setRequestProperty("Referer", "https://music.163.com/m/song?id=16431842");
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36");
connection.setRequestProperty("Origin","http://music.163.com");
connection.setRequestProperty("Connection","keep-alive");
connection.setRequestProperty("Accept-Language","zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7");
connection.connect();// 连接会话
// 获取输入流
PrintWriter writer = new PrintWriter(connection.getOutputStream());
writer.print(data);
writer.flush();
BufferedReader br= new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null)
{// 循环读取流
sb.append(line);
}
br.close();// 关闭流
connection.disconnect();// 断开连接
return sb.toString();
}
catch (Exception e)
{
System.out.println(e.toString());
}
return null;
}
public static String USER_AGENT = "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.0)";
//爬虫爬取ssl链接忽略SSL
static public void init() {
try {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new X509TrustManager[]{new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}}, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
} catch (NoSuchAlgorithmException e) {
} catch (KeyManagementException e) {
}
}
public static String Curl(String url)
{
try
{
InputStream is=new URL(url).openStream();
ByteArrayOutputStream buffer=new ByteArrayOutputStream();
int b=-1;
while ((b = is.read()) != -1)
buffer.write(b);
return new String(buffer.toByteArray());
}
catch (MalformedURLException e)
{
System.out.println(e.toString());
}
catch (IOException e)
{
System.out.println(e.toString());
}
return null;
}
public static String ridiculous_uin(String qquin)
{
String uin = String.valueOf(qquin);
return uin.replace("0","⓪")
.replace("1","①")
.replace("2","②")
.replace("3","③")
.replace("4","④")
.replace("5","⑤")
.replace("6","⑥")
.replace("7","⑦")
.replace("8","⑧")
.replace("9","⑨");
}
public static HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session)
{
return true;
}
};
public static void trustAllHttpsCertificates() throws Exception
{
TrustManager[] trustAllCerts = new TrustManager[1];
TrustManager tm = new miTM();
trustAllCerts[0] = tm;
SSLContext sc = SSLContext
.getInstance("SSL");
sc.init(null, trustAllCerts, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sc
.getSocketFactory());
}
public static class miTM implements TrustManager,
X509TrustManager
{
public X509Certificate[] getAcceptedIssuers()
{
return null;
}
public static boolean isServerTrusted(
X509Certificate[] certs)
{
return true;
}
public static boolean isClientTrusted(
X509Certificate[] certs)
{
return true;
}
public void checkServerTrusted(
X509Certificate[] certs, String authType)
throws CertificateException
{
return;
}
public void checkClientTrusted(
X509Certificate[] certs, String authType)
throws CertificateException
{
return;
}
}
}