import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sun.org.apache.xerces.internal.util.URI;
import javax.net.ssl.*;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class RequestTemplate {
static String[] user= new String[]{
"Mozilla/5.0 (compatible; Baiduspider/2.0;+http://www.baidu.com/search/spider.html) safari 5.1 – MAC",
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
"User-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
"Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)",};
public static int agent(){
Random random =new Random();
return random.nextInt(user.length);
}
static {
TrustManager[] trustManager=new TrustManager[]{new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}};
try{
SSLContext sslContext=SSLContext.getInstance("TLS");
sslContext.init(null,trustManager,new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
}catch (Exception e){
}
}
private final static HostnameVerifier Do_NOT_VERIFY = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
public static Map<String,Object> GetBody(Map<String ,String > requestproprety, String uri,int timeout){
boolean isHttps= true;
if (uri.indexOf("Http")==-1&&uri.indexOf("Https")==-1){
HttpsURLConnection httpsURLConnection =null;
try{
URL url =new URL("Https://"+uri);
httpsURLConnection=(HttpsURLConnection) url.openConnection();
httpsURLConnection.setHostnameVerifier(Do_NOT_VERIFY);
httpsURLConnection.setConnectTimeout(2000);
httpsURLConnection.setReadTimeout(1000);
httpsURLConnection.connect();
}catch (Exception E){
isHttps=false;
}finally {
if (isHttps){
uri="Https://"+uri;
}else {
uri="Http://"+uri;
}
if (null!=httpsURLConnection){
httpsURLConnection.disconnect();
}
}
}
Map<String,Object> response=new HashMap<String, Object>();
StringBuilder result= new StringBuilder();
HttpURLConnection connection =null;
try {
URL url = new URL(uri);
if (url.getProtocol().toLowerCase().equals("https:")){
HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
https.setHostnameVerifier(Do_NOT_VERIFY);
connection=https;
}else {
connection= (HttpURLConnection) url.openConnection();
}
if (requestproprety.get("User-Agent")==null){
connection.setRequestProperty("User-Agent",user[agent()]);
}
if (null==requestproprety.get("Content-Type")){
connection.setRequestProperty("Content-Type","applocation/x-www-form-urlencode;charset=UTF-8");
}
for (Map.Entry<String,String> entry: requestproprety.entrySet()){
connection.setRequestProperty(entry.getKey(),entry.getValue());
}
connection.setConnectTimeout(timeout);
connection.setReadTimeout(timeout);
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.connect();
int rescode=connection.getResponseCode();
Map<String, List<String>> resproperty = connection.getHeaderFields();
InputStreamReader inputStreamReader=null;
if (rescode==200){
inputStreamReader=new InputStreamReader(connection.getInputStream(),"UTF-8");
}else {
inputStreamReader=new InputStreamReader(connection.getErrorStream(),"UTF-8");
}
if (inputStreamReader!=null){
BufferedReader br =new BufferedReader(inputStreamReader);
String line=null;
while ((line=br.readLine())!=null){
result.append(line+"\n");
if (line==null){
inputStreamReader.close();
}
}
}
connection.disconnect();
response.put("Code",rescode);
response.put("Content",result.toString());
response.put("resproperty",resproperty);
}catch (Exception e){
e.printStackTrace();
}
return response;
}
public static Map<String,Object> GetBodyparam(Map<String ,String > requestproprety, String uri,String param,int timeout){
boolean isHttps= true;
if (uri.indexOf("Http")==-1&&uri.indexOf("Https")==-1){
HttpsURLConnection httpsURLConnection =null;
try{
URL url =new URL("Https://"+uri);
httpsURLConnection=(HttpsURLConnection) url.openConnection();
httpsURLConnection.setHostnameVerifier(Do_NOT_VERIFY);
httpsURLConnection.setConnectTimeout(2000);
httpsURLConnection.setReadTimeout(1000);
httpsURLConnection.connect();
}catch (Exception E){
isHttps=false;
}finally {
if (isHttps){
uri="Https://"+uri+"?"+param;
}else {
uri="Http://"+uri+"?"+param;
}
if (null!=httpsURLConnection){
httpsURLConnection.disconnect();
}
}
}
System.out.println(uri);
Map<String,Object> response=new HashMap<String, Object>();
StringBuilder result= new StringBuilder();
HttpURLConnection connection =null;
try {
URL url = new URL(uri);
if (url.getProtocol().toLowerCase().equals("https:")){
HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
https.setHostnameVerifier(Do_NOT_VERIFY);
connection=https;
}else {
connection= (HttpURLConnection) url.openConnection();
}
if (requestproprety.get("User-Agent")==null){
connection.setRequestProperty("User-Agent",user[agent()]);
}
if (null==requestproprety.get("Content-Type")){
connection.setRequestProperty("Content-Type","applocation/x-www-form-urlencode;charset=UTF-8");
}
for (Map.Entry<String,String> entry: requestproprety.entrySet()){
connection.setRequestProperty(entry.getKey(),entry.getValue());
}
connection.setConnectTimeout(timeout);
connection.setReadTimeout(timeout);
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.connect();
int rescode=connection.getResponseCode();
Map<String, List<String>> resproperty = connection.getHeaderFields();
InputStreamReader inputStreamReader=null;
if (rescode==200){
inputStreamReader=new InputStreamReader(connection.getInputStream(),"UTF-8");
}else {
inputStreamReader=new InputStreamReader(connection.getErrorStream(),"UTF-8");
}
if (inputStreamReader!=null){
BufferedReader br =new BufferedReader(inputStreamReader);
String line=null;
while ((line=br.readLine())!=null){
result.append(line+"\n");
if (line==null){
inputStreamReader.close();
}
}
}
connection.disconnect();
response.put("Code",rescode);
response.put("Content",result.toString());
response.put("resproperty",resproperty);
}catch (Exception e){
e.printStackTrace();
}
return response;
}
public static Map<String,Object> PostBody(Map<String ,String > requestproprety, String uri,String param,int timeout){
boolean isHttps= true;
if (uri.indexOf("Http")==-1&&uri.indexOf("Https")==-1){
HttpsURLConnection httpsURLConnection =null;
try{
URL url =new URL("Https://"+uri);
httpsURLConnection=(HttpsURLConnection) url.openConnection();
httpsURLConnection.setHostnameVerifier(Do_NOT_VERIFY);
httpsURLConnection.setConnectTimeout(2000);
httpsURLConnection.setReadTimeout(1000);
httpsURLConnection.connect();
}catch (Exception E){
isHttps=false;
}finally {
if (isHttps){
uri="Https://"+uri+"?"+param;
}else {
uri="Http://"+uri+"?"+param;
}
if (null!=httpsURLConnection){
httpsURLConnection.disconnect();
}
}
}
Map<String,Object> response=new HashMap<String, Object>();
StringBuilder result= new StringBuilder();
HttpURLConnection connection =null;
try {
URL url = new URL(uri);
if (url.getProtocol().toLowerCase().equals("https:")){
HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
https.setHostnameVerifier(Do_NOT_VERIFY);
connection=https;
}else {
connection= (HttpURLConnection) url.openConnection();
}
if (requestproprety.get("User-Agent")==null){
connection.setRequestProperty("User-Agent",user[agent()]);
}
if (null==requestproprety.get("Content-Type")){
connection.setRequestProperty("Content-Type","applocation/x-www-form-urlencode;charset=UTF-8");
}
for (Map.Entry<String,String> entry: requestproprety.entrySet()){
connection.setRequestProperty(entry.getKey(),entry.getValue());
}
connection.setConnectTimeout(timeout);
connection.setReadTimeout(timeout);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
PrintWriter printWriter =new PrintWriter(new BufferedOutputStream(connection.getOutputStream()));
printWriter.write(param);
printWriter.flush();
printWriter.close();
int rescode=connection.getResponseCode();
Map<String, List<String>> resproperty = connection.getHeaderFields();
InputStreamReader inputStreamReader=null;
if (rescode==200){
inputStreamReader=new InputStreamReader(connection.getInputStream(),"UTF-8");
}else {
inputStreamReader=new InputStreamReader(connection.getErrorStream(),"UTF-8");
}
if (inputStreamReader!=null){
BufferedReader br =new BufferedReader(inputStreamReader);
String line=null;
while ((line=br.readLine())!=null){
result.append(line+"\n");
if (line==null){
inputStreamReader.close();
}
}
}
connection.disconnect();
response.put("Code",rescode);
response.put("Content",result.toString());
response.put("resproperty",resproperty);
}catch (Exception e){
e.printStackTrace();
}
return response;
}
public static Map<String,Object> JsonBody(Map<String ,String > requestproprety, String uri,String param,int timeout){
boolean isHttps= true;
if (uri.indexOf("Http")==-1&&uri.indexOf("Https")==-1){
HttpsURLConnection httpsURLConnection =null;
try{
URL url =new URL("Https://"+uri);
httpsURLConnection=(HttpsURLConnection) url.openConnection();
httpsURLConnection.setHostnameVerifier(Do_NOT_VERIFY);
httpsURLConnection.setConnectTimeout(2000);
httpsURLConnection.setReadTimeout(1000);
httpsURLConnection.connect();
}catch (Exception E){
isHttps=false;
}finally {
if (isHttps){
uri="Https://"+uri+"?"+param;
}else {
uri="Http://"+uri+"?"+param;
}
if (null!=httpsURLConnection){
httpsURLConnection.disconnect();
}
}
}
Map<String,Object> response=new HashMap<String, Object>();
StringBuilder result= new StringBuilder();
HttpURLConnection connection =null;
try {
URL url = new URL(uri);
if (url.getProtocol().toLowerCase().equals("https:")){
HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
https.setHostnameVerifier(Do_NOT_VERIFY);
connection=https;
}else {
connection= (HttpURLConnection) url.openConnection();
}
if (requestproprety.get("User-Agent")==null){
connection.setRequestProperty("User-Agent",user[agent()]);
}
if (null==requestproprety.get("Content-Type")){
connection.setRequestProperty("Content-Type","applocation/json;charset=UTF-8");
}
for (Map.Entry<String,String> entry: requestproprety.entrySet()){
connection.setRequestProperty(entry.getKey(),entry.getValue());
}
connection.setConnectTimeout(timeout);
connection.setReadTimeout(timeout);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
PrintWriter printWriter =new PrintWriter(new BufferedOutputStream(connection.getOutputStream()));
printWriter.write(param);
printWriter.flush();
printWriter.close();
int rescode=connection.getResponseCode();
Map<String, List<String>> resproperty = connection.getHeaderFields();
InputStreamReader inputStreamReader=null;
if (rescode==200){
inputStreamReader=new InputStreamReader(connection.getInputStream(),"UTF-8");
}else {
inputStreamReader=new InputStreamReader(connection.getErrorStream(),"UTF-8");
}
if (inputStreamReader!=null){
BufferedReader br =new BufferedReader(inputStreamReader);
String line=null;
while ((line=br.readLine())!=null){
result.append(line+"\n");
if (line==null){
inputStreamReader.close();
}
}
}
connection.disconnect();
response.put("Code",rescode);
response.put("Content",result.toString());
response.put("resproperty",resproperty);
}catch (Exception e){
e.printStackTrace();
}
return response;
}
public static boolean ceye( String filter){
boolean flag=false;
String token="xxxxxxx";
Map<String ,Object> ceyeobject=GetBodyparam(new HashMap<String, String>(),"api.ceye.io/v1/records","token="+token+"&type=dns&filter="+filter,8000);
Object content=ceyeobject.get("Content");
System.out.println(content);
JSONObject jsonObject = JSON.parseObject((String) content);
List data =(List)jsonObject.get("data");
if (data.size()>0)
return flag=true;
else
return flag;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?