| import com.alibaba.fastjson.JSON; |
| import com.alibaba.fastjson.JSONObject; |
| import lombok.extern.slf4j.Slf4j; |
| |
| import javax.net.ssl.*; |
| import java.io.*; |
| import java.net.HttpURLConnection; |
| import java.net.URL; |
| import java.net.URLConnection; |
| import java.security.KeyManagementException; |
| import java.security.NoSuchAlgorithmException; |
| import java.security.SecureRandom; |
| 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.Set; |
| |
| @Slf4j |
| public class HttpUtils { |
| |
| public static void main(String[] args) { |
| String res = sendGetRequest("https://desk.zol.com.cn/"); |
| log.info(res); |
| } |
| |
| |
| private static final class DefaultTrustManager implements 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 null; |
| } |
| } |
| |
| private static HttpsURLConnection getHttpsURLConnection(String uri, String method) throws IOException { |
| SSLContext ctx = null; |
| try { |
| ctx = SSLContext.getInstance("TLS"); |
| ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom()); |
| } catch (KeyManagementException | NoSuchAlgorithmException e) { |
| e.printStackTrace(); |
| } |
| SSLSocketFactory ssf = ctx.getSocketFactory(); |
| |
| |
| |
| URL url = new URL(null,uri,new sun.net.www.protocol.https.Handler()); |
| HttpsURLConnection httpsConn = (HttpsURLConnection) url.openConnection(); |
| httpsConn.setSSLSocketFactory(ssf); |
| httpsConn.setHostnameVerifier(new HostnameVerifier() { |
| @Override |
| public boolean verify(String arg0, SSLSession arg1) { |
| return true; |
| } |
| }); |
| httpsConn.setRequestMethod(method); |
| httpsConn.setDoInput(true); |
| httpsConn.setDoOutput(true); |
| return httpsConn; |
| } |
| |
| private static byte[] getBytesFromStream(InputStream is) throws IOException { |
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| byte[] kb = new byte[1024]; |
| int len; |
| while ((len = is.read(kb)) != -1) { |
| baos.write(kb, 0, len); |
| } |
| byte[] bytes = baos.toByteArray(); |
| baos.close(); |
| is.close(); |
| return bytes; |
| } |
| |
| private static void setBytesToStream(OutputStream os, byte[] bytes) throws IOException { |
| ByteArrayInputStream bais = new ByteArrayInputStream(bytes); |
| byte[] kb = new byte[1024]; |
| int len; |
| while ((len = bais.read(kb)) != -1) { |
| os.write(kb, 0, len); |
| } |
| os.flush(); |
| os.close(); |
| bais.close(); |
| } |
| |
| public static byte[] doGet(String uri) throws IOException { |
| HttpsURLConnection httpsConn = getHttpsURLConnection(uri, "GET"); |
| return getBytesFromStream(httpsConn.getInputStream()); |
| } |
| |
| public static byte[] doPost(String uri, String data, String token) throws IOException { |
| HttpsURLConnection httpsConn = getHttpsURLConnection(uri, "POST"); |
| httpsConn.setRequestProperty("Content-Type", "application/json"); |
| if (!"".equals(token) && token != null) { |
| httpsConn.setRequestProperty("x-access-token", token); |
| } |
| setBytesToStream(httpsConn.getOutputStream(), data.getBytes("gbk")); |
| return getBytesFromStream(httpsConn.getInputStream()); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| public static String sendGet(String url, String charset) throws Exception { |
| URL realurl = new URL(url); |
| HttpURLConnection con = (HttpURLConnection) realurl.openConnection(); |
| |
| |
| con.setRequestMethod("GET"); |
| |
| |
| String USER_AGENT = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; customie8)"; |
| con.setRequestProperty("User-Agent", USER_AGENT); |
| BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), charset)); |
| String inputLine; |
| StringBuilder result = new StringBuilder(); |
| while ((inputLine = in.readLine()) != null) { |
| result.append(inputLine); |
| result.append("\r\n"); |
| } |
| in.close(); |
| con.disconnect(); |
| return result.toString(); |
| } |
| |
| |
| |
| |
| |
| |
| |
| public static String sendGetRequest(String url) { |
| String result = ""; |
| BufferedReader in = null; |
| try { |
| URL realUrl = new URL(url); |
| |
| HttpURLConnection connection = (HttpURLConnection) 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(); |
| |
| |
| 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 { |
| try { |
| if (in != null) { |
| in.close(); |
| } |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| } |
| return result; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| public static String sendGetRequest(String url, List<HashMap<String, String>> param) { |
| String result = ""; |
| BufferedReader in = null; |
| try { |
| StringBuilder stringBuilder = new StringBuilder(); |
| stringBuilder.append(url); |
| stringBuilder.append("?"); |
| for (HashMap<String, String> item : param) { |
| Set<Map.Entry<String, String>> entrySet = item.entrySet(); |
| for (Map.Entry<String, String> entry : entrySet) { |
| stringBuilder.append(entry.getKey()); |
| stringBuilder.append("="); |
| stringBuilder.append(entry.getValue()); |
| stringBuilder.append("&"); |
| } |
| } |
| stringBuilder.substring(0,stringBuilder.length()-1); |
| String totalUrl = stringBuilder.substring(0,stringBuilder.length()-1).toString(); |
| |
| URL realUrl = new URL(totalUrl); |
| |
| HttpURLConnection connection = (HttpURLConnection) 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(); |
| |
| |
| 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 { |
| try { |
| if (in != null) { |
| in.close(); |
| } |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| } |
| return result; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| public static JSONObject sendGet(String url, List<HashMap<String, String>> param) { |
| String result = ""; |
| BufferedReader in = null; |
| try { |
| StringBuilder stringBuilder = new StringBuilder(); |
| stringBuilder.append(url); |
| stringBuilder.append("?"); |
| for (HashMap<String, String> item : param) { |
| Set<Map.Entry<String, String>> entrySet = item.entrySet(); |
| for (Map.Entry<String, String> entry : entrySet) { |
| stringBuilder.append(entry.getKey()); |
| stringBuilder.append("="); |
| stringBuilder.append(entry.getValue()); |
| stringBuilder.append("&"); |
| } |
| } |
| stringBuilder.substring(0,stringBuilder.length()-1); |
| String totalUrl = stringBuilder.substring(0,stringBuilder.length()-1).toString(); |
| |
| log.info(totalUrl); |
| URL realUrl = new URL(totalUrl); |
| |
| HttpURLConnection connection = (HttpURLConnection) 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(); |
| |
| |
| if(connection.getResponseCode() == 200){ |
| |
| in = new BufferedReader(new InputStreamReader( |
| connection.getInputStream(), "utf-8")); |
| String line; |
| while ((line = in.readLine()) != null) { |
| result += line; |
| } |
| }else { |
| |
| in = new BufferedReader(new InputStreamReader( |
| connection.getErrorStream(), "utf-8")); |
| String line; |
| while ((line = in.readLine()) != null) { |
| result += line; |
| } |
| } |
| } catch (Exception e) { |
| System.out.println("发送GET请求出现异常!" + e); |
| e.printStackTrace(); |
| } |
| |
| finally { |
| try { |
| if (in != null) { |
| in.close(); |
| } |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| return JSON.parseObject(result); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| public static String sendPost(String url, String param) throws Exception { |
| PrintWriter out; |
| BufferedReader in; |
| StringBuilder result = new StringBuilder(); |
| URL realUrl = new URL(url); |
| |
| URLConnection conn = realUrl.openConnection(); |
| |
| conn.setRequestProperty("accept", "*/*"); |
| conn.setRequestProperty("connection", "Keep-Alive"); |
| conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); |
| |
| conn.setDoOutput(true); |
| conn.setDoInput(true); |
| |
| out = new PrintWriter(conn.getOutputStream()); |
| |
| out.print(param); |
| |
| out.flush(); |
| |
| in = new BufferedReader(new InputStreamReader(conn.getInputStream())); |
| String line; |
| while ((line = in.readLine()) != null) { |
| result.append(line); |
| } |
| out.close(); |
| in.close(); |
| return result.toString(); |
| } |
| |
| |
| |
| |
| |
| |
| |
| public static String sendPost(String url, HashMap<String, String> params) { |
| OutputStreamWriter out = null; |
| BufferedReader in = null; |
| StringBuilder result = new StringBuilder(); |
| try { |
| URL realUrl = new URL(url); |
| HttpURLConnection conn = (HttpURLConnection) realUrl |
| .openConnection(); |
| |
| conn.setDoOutput(true); |
| conn.setDoInput(true); |
| |
| conn.setRequestMethod("POST"); |
| |
| conn.setRequestProperty("accept", "*/*"); |
| conn.setRequestProperty("connection", "Keep-Alive"); |
| conn.setRequestProperty("user-agent", |
| "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); |
| conn.setRequestProperty("Content-Type", |
| "application/x-www-form-urlencoded"); |
| conn.connect(); |
| |
| out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); |
| |
| if (params != null) { |
| StringBuilder param = new StringBuilder(); |
| for (HashMap.Entry<String, String> entry : params.entrySet()) { |
| if (param.length() > 0) { |
| param.append("&"); |
| } |
| param.append(entry.getKey()); |
| param.append("="); |
| param.append(entry.getValue()); |
| |
| } |
| System.out.println("param:" + param.toString()); |
| out.write(param.toString()); |
| } |
| |
| out.flush(); |
| |
| in = new BufferedReader(new InputStreamReader( |
| conn.getInputStream(), "UTF-8")); |
| String line; |
| while ((line = in.readLine()) != null) { |
| result.append(line); |
| } |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| |
| finally { |
| try { |
| if (out != null) { |
| out.close(); |
| } |
| if (in != null) { |
| in.close(); |
| } |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| } |
| return result.toString(); |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探