HTTP请求处理 get/post工具类 验证网络DEMO

简介: HTTP请求处理 get/post工具类 验证网络DEMO

HTTP请求处理 get/post工具类 验证网络DEMO

package com.aostar.ida.framework.util;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import oracle.net.aso.p;
/**
 * * HTTP请求处理 get/post
 * 
 * @author 
 *
 */
public class HttpUtilYan {
    private final static Logger logger = LoggerFactory.getLogger(HttpUtils.class);
    private static final Charset UTF_8 = Charset.forName("UTF-8");
    public static final String POST = "POST";
    public static final String GET = "GET";
    public static final int TIMEOUT = 30000;
    public static final int FILE_TIMEOUT = 300000;
    // boundary就是request头和上传文件内容的分隔符
    public static final String BOUNDARY = "---------------------------123821742118716";
    public static final String RESULT_FILE = "attachment";
    // Http Get请求
    //
    /**
     * * 发送HTTP请求
     * 
     * @param serverUrl
     * @param params
     * @return
     */
    public static String getHttp(String serverUrl, Map<String, String> params) {
        return getHttp(serverUrl, params, null, TIMEOUT);
    }
    /**
     * * 发送HTTP请求
     * 
     * @param serverUrl
     * @param params
     * @return
     */
    public static String getHttp(String serverUrl, Map<String, String> params, Map<String, String> headers) {
        return getHttp(serverUrl, params, headers, TIMEOUT);
    }
    /**
     * 发送HTTP请求
     * 
     * @param serverUrl
     * @param method
     * @param params
     * @param timeout
     * @return
     */
    public static String getHttp(String serverUrl, Map<String, String> params, Map<String, String> headers,
            Integer time) {
        logger.info("\n【请求地址】: {} \n【请求参数】: {} \n【请求头部】: {}", serverUrl, params, headers);
        HttpURLConnection conn = null;
        BufferedReader br = null;
        try {
            // 构建请求参数
            StringBuilder sbParam = new StringBuilder(serverUrl);
            if (mapIsNotEmpty(params)) {
                sbParam.append("?");
                if (mapIsNotEmpty(params)) {
                    for (Map.Entry<String, String> entry : params.entrySet()) {
                        sbParam.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
                        sbParam.append("=");
                        sbParam.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
                        sbParam.append("&");
                    }
                }
            }
            // 发送请求
            URL url = new URL(sbParam.toString());
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            conn.setRequestMethod(GET);
            conn.setUseCaches(false);
            conn.setDoOutput(true);
            conn.setReadTimeout(time);
            conn.setConnectTimeout(time);
            if (mapIsNotEmpty(headers)) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    conn.setRequestProperty(entry.getKey(), entry.getValue());
                }
            }
            logger.info("[HttpsUtls] [getHttp] connection success");
            // 获取状态码
            int responseCode = conn.getResponseCode();
            if (responseCode != HttpURLConnection.HTTP_OK) {
                logger.error("[HttpsUtls] [getHttp] response status = {}", responseCode);
                return StringUtils.EMPTY;
            }
            // 取得返回信息
            br = new BufferedReader(new InputStreamReader(conn.getInputStream(), UTF_8));
            String line;
            StringBuilder result = new StringBuilder();
            while ((line = br.readLine()) != null) {
                result.append(line);
            }
            logger.info("\n【响应数据】: {} ", result);
            // JSON处理
            return result.toString();
        } catch (Exception e) {
            logger.error("[HttpsUtls] [getHttp Exception", e);
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (Exception e) {
                    logger.error("[HttpsUtls] [getHttp] br.close is exception. {}", e);
                }
            }
            if (conn != null) {
                try {
                    conn.disconnect();
                } catch (Exception e) {
                    logger.error("[HttpsUtls] [getHttp] conn.disconnect() is exception. {}", e);
                }
            }
        }
        return StringUtils.EMPTY;
    }
    // HTTP POST请求
    ///
    /**
     * POST 请求
     * 
     * @param serverUrl
     * @param params
     * @return
     */
    public static String postHttp(String serverUrl, Map<String, String> params) {
        return postHttp(serverUrl, params, null);
    }
    /**
     * POST 请求
     * 
     * @param serverUrl
     * @param params
     * @param headers
     * @return
     */
    public static String postHttp(String serverUrl, Map<String, String> params, Map<String, String> headers) {
        return postHttp(serverUrl, params, headers, POST, TIMEOUT);
    }
    /**
     * 发送HTTP请求
     * 
     * @param serverUrl
     * @param method
     * @param params
     * @param timeout
     * @return
     */
    public static String postHttp(String serverUrl, Map<String, String> params, Map<String, String> headers,
            String type, int timeout) {
        logger.info("\n【请求地址】: {} \n【请求参数】: {} \n【请求头部】: {}", serverUrl, params, headers);
        HttpURLConnection conn = null;
        OutputStreamWriter osw = null;
        BufferedReader br = null;
        try {
            // 发送请求
            URL url = new URL(serverUrl);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            conn.setRequestMethod(type);
            conn.setUseCaches(false);
            conn.setDoOutput(true);
            conn.setReadTimeout(timeout);
            conn.setConnectTimeout(timeout);
            if (mapIsNotEmpty(headers)) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    conn.setRequestProperty(entry.getKey(), entry.getValue());
                }
            }
            // 构建请求参数
            StringBuilder sbParam = new StringBuilder();
            if (mapIsNotEmpty(params)) {
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    sbParam.append(entry.getKey());
                    sbParam.append("=");
                    sbParam.append(entry.getValue());
                    sbParam.append("&");
                }
            }
            // 获得输出流
            osw = new OutputStreamWriter(conn.getOutputStream(), UTF_8);
            osw.write(sbParam.toString());
            osw.flush();
            osw.close();
            logger.info("[HttpsUtls][postHttp] connection success.");
            // 获取状态码
            int responseCode = conn.getResponseCode();
            if (responseCode != HttpURLConnection.HTTP_OK) {
                logger.error("[HttpsUtls][postHttp] response status = {}", responseCode);
                return StringUtils.EMPTY;
            }
            // 取得返回信息
            br = new BufferedReader(new InputStreamReader(conn.getInputStream(), UTF_8));
            String line;
            StringBuilder result = new StringBuilder();
            while ((line = br.readLine()) != null) {
                result.append(line);
            }
            logger.info("\n【响应数据】: {} ", result);
            // JSON处理
            return result.toString();
        } catch (Exception e) {
            logger.error("[HttpsUtls][postHttp] Exception", e);
        } finally {
            if (osw != null) {
                try {
                    osw.close();
                } catch (Exception e) {
                    logger.error("[HttpsUtls][postHttp] osw.close() is Exception. {}", e);
                }
            }
            if (br != null) {
                try {
                    br.close();
                } catch (Exception e) {
                    logger.error("[HttpsUtls][postHttp] br.close() is Exception. {}", e);
                }
            }
            if (conn != null) {
                try {
                    conn.disconnect();
                } catch (Exception e) {
                    logger.error("[HttpsUtls][postHttp] conn.disconnect() is Exception. {}", e);
                }
            }
        }
        return StringUtils.EMPTY;
    }
    // HTTP 推送JSON格式
    ///
    /**
     * Post 请求 格式为 application/json
     * 
     * @param serverUrl
     * @param urlParams
     * @param headers
     * @param jsonParam
     * @return
     */
    public static String postJson(String serverUrl, Map<String, String> urlParams, Map<String, String> headers,
            String jsonParam) {
        logger.info("\n【请求地址】: {} \n【请求参数】: {} \n【请求头部】: {}\n【请求JSON】: {}", serverUrl, urlParams, headers, jsonParam);
        HttpURLConnection conn = null;
        OutputStreamWriter osw = null;
        BufferedReader br = null;
        try {
            // 构建请求参数
            StringBuilder sbParam = new StringBuilder(serverUrl);
            if (mapIsNotEmpty(urlParams)) {
                sbParam.append("?");
                if (mapIsNotEmpty(urlParams)) {
                    for (Map.Entry<String, String> entry : urlParams.entrySet()) {
                        sbParam.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
                        sbParam.append("=");
                        sbParam.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
                        sbParam.append("&");
                    }
                }
            }
            // 发送请求
            URL url = new URL(sbParam.toString());
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            if (mapIsNotEmpty(headers)) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    conn.setRequestProperty(entry.getKey(), entry.getValue());
                }
            }
            conn.setRequestMethod(POST);
            conn.setUseCaches(false);
            conn.setDoOutput(true);
            conn.setReadTimeout(TIMEOUT);
            conn.setConnectTimeout(TIMEOUT);
            // 设置文件类型:
            conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            // 设置接收类型否则返回415错误
            conn.setRequestProperty("accept", "application/json");
            // 往服务器里面发送数据
            if (jsonParam != null) {
                byte[] writebytes = jsonParam.getBytes();
                // 设置文件长度
                conn.setRequestProperty("Content-Length", String.valueOf(writebytes.length));
            }
            // 获得输出流
            osw = new OutputStreamWriter(conn.getOutputStream(), UTF_8);
            osw.write(jsonParam);
            osw.flush();
            osw.close();
            logger.info("[HttpsUtls][postJson] connection success.");
            // 构建请求参数
            sbParam = new StringBuilder();
            int responseCode = conn.getResponseCode();
            if (responseCode != HttpURLConnection.HTTP_OK) {
                logger.error("[HttpsUtls][postJson] responseCode:{}", responseCode);
                return StringUtils.EMPTY;
            }
            // 取得返回信息
            br = new BufferedReader(new InputStreamReader(conn.getInputStream(), UTF_8));
            String line;
            StringBuilder result = new StringBuilder();
            while ((line = br.readLine()) != null) {
                result.append(line);
            }
            logger.info("\n【响应数据】: {} ", result);
            // JSON处理
            return result.toString();
        } catch (Exception e) {
            logger.error("[HttpsUtls][postJson] Exception", e);
        } finally {
            if (osw != null) {
                try {
                    osw.close();
                } catch (Exception e) {
                    logger.error("[HttpsUtls][postJson] osw.close() is Exception. {}", e);
                }
            }
            if (br != null) {
                try {
                    br.close();
                } catch (Exception e) {
                    logger.error("[HttpsUtls][postJson] br.close() is Exception. {}", e);
                }
            }
            if (conn != null) {
                try {
                    conn.disconnect();
                } catch (Exception e) {
                    logger.error("[HttpsUtls][postJson] conn.disconnect() is Exception. {}", e);
                }
            }
        }
        return StringUtils.EMPTY;
    }
    /**
     * post 请求 格式为 text/plain
     * 
     * @param serverUrl
     * @param urlParams
     * @param headers
     * @param jsonParam
     * @return
     */
    public static String postText(String serverUrl, Map<String, String> urlParams, Map<String, String> headers,
            String jsonParam) {
        logger.info("\n【请求地址】: {} \n【请求参数】: {} \n【请求头部】: {}\n【请求JSON】: {}", serverUrl, urlParams, headers, jsonParam);
        HttpURLConnection conn = null;
        OutputStreamWriter osw = null;
        BufferedReader br = null;
        try {
            // 构建请求参数
            StringBuilder sbParam = new StringBuilder(serverUrl);
            if (mapIsNotEmpty(urlParams)) {
                sbParam.append("?");
                if (mapIsNotEmpty(urlParams)) {
                    for (Map.Entry<String, String> entry : urlParams.entrySet()) {
                        sbParam.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
                        sbParam.append("=");
                        sbParam.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
                        sbParam.append("&");
                    }
                }
            }
            // 发送请求
            URL url = new URL(sbParam.toString());
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            if (mapIsNotEmpty(headers)) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    conn.setRequestProperty(entry.getKey(), entry.getValue());
                }
            }
            conn.setRequestMethod(POST);
            conn.setUseCaches(false);
            conn.setDoOutput(true);
            conn.setReadTimeout(TIMEOUT);
            conn.setConnectTimeout(TIMEOUT);
            // 设置文件类型:
            conn.setRequestProperty("Content-Type", "text/plain; charset=UTF-8");
            // 设置接收类型否则返回415错误
            conn.setRequestProperty("accept", "application/json");
            // 往服务器里面发送数据
            if (jsonParam != null) {
                byte[] writebytes = jsonParam.getBytes();
                // 设置文件长度
                conn.setRequestProperty("Content-Length", String.valueOf(writebytes.length));
            }
            // 获得输出流
            osw = new OutputStreamWriter(conn.getOutputStream(), UTF_8);
            osw.write(jsonParam);
            osw.flush();
            osw.close();
            logger.info("[HttpsUtls][postText] connection success.");
            // 构建请求参数
            sbParam = new StringBuilder();
            int responseCode = conn.getResponseCode();
            if (responseCode != HttpURLConnection.HTTP_OK) {
                logger.error("[HttpsUtls][postText] responseCode:{}", responseCode);
                return StringUtils.EMPTY;
            }
            // 取得返回信息
            br = new BufferedReader(new InputStreamReader(conn.getInputStream(), UTF_8));
            String line;
            StringBuilder result = new StringBuilder();
            while ((line = br.readLine()) != null) {
                result.append(line);
            }
            logger.info("\n【响应数据】: {} ", result);
            // JSON处理
            return result.toString();
        } catch (Exception e) {
            logger.error("[HttpsUtls][postText] Exception", e);
        } finally {
            if (osw != null) {
                try {
                    osw.close();
                } catch (Exception e) {
                    logger.error("[HttpsUtls][postText] osw.close() is Exception. {}", e);
                }
            }
            if (br != null) {
                try {
                    br.close();
                } catch (Exception e) {
                    logger.error("[HttpsUtls][postText] br.close() is Exception. {}", e);
                }
            }
            if (conn != null) {
                try {
                    conn.disconnect();
                } catch (Exception e) {
                    logger.error("[HttpsUtls][postText] conn.disconnect() is Exception. {}", e);
                }
            }
        }
        return StringUtils.EMPTY;
    }
    /**
     * * 上传多个文件
     * 
     * @param urlStr
     * @param urlParams
     * @param fileMap
     * @return
     */
    public static String formUpload(String urlStr, Map<String, String> urlParams, Map<String, String> fileMap) {
        if (mapIsEmpty(fileMap)) {
            logger.info("\n【请求地址】: {} \n【请求参数】: {} ", urlStr, urlParams);
        } else {
            logger.info("\n【请求地址】: {} \n【请求参数】: {} \n【请求文件】: {}", urlStr, urlParams, fileMap);
        }
        String res = "";
        HttpURLConnection conn = null;
        DataInputStream in = null;
        try {
            URL url = new URL(urlStr);
            conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(FILE_TIMEOUT);
            conn.setReadTimeout(FILE_TIMEOUT);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
            OutputStream out = new DataOutputStream(conn.getOutputStream());
            // urlParams
            if (urlParams != null) {
                StringBuilder strBuf = new StringBuilder();
                Iterator<Map.Entry<String, String>> iter = urlParams.entrySet().iterator();
                while (iter.hasNext()) {
                    Map.Entry<String, String> entry = iter.next();
                    String inputName = entry.getKey();
                    String inputValue = entry.getValue();
                    if (inputValue == null) {
                        continue;
                    }
                    strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
                    strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n");
                    strBuf.append(inputValue);
                }
                out.write(strBuf.toString().getBytes());
            }
            // file
            if (fileMap != null) {
                Iterator<Map.Entry<String, String>> iter = fileMap.entrySet().iterator();
                while (iter.hasNext()) {
                    Map.Entry<String, String> entry = iter.next();
                    String inputName = entry.getKey();
                    String inputValue = entry.getValue();
                    if (inputValue == null) {
                        continue;
                    }
                    File file = new File(inputValue);
                    if (!file.exists()) {
                        logger.error("[HttpsUtls][formUpload] file is not exist. name = {}", inputValue);
                        return res;
                    }
                    String filename = file.getName();
                    StringBuilder strBuf = new StringBuilder();
                    strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
                    strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"; filename=\"" + filename
                            + "\"\r\n");
                    strBuf.append("Content-Type:" + "multipart/form-data;boundary=" + "*****" + "\r\n\r\n");
                    out.write(strBuf.toString().getBytes());
                    in = new DataInputStream(new FileInputStream(file));
                    int bytes = 0;
                    byte[] bufferOut = new byte[1024];
                    while ((bytes = in.read(bufferOut)) != -1) {
                        out.write(bufferOut, 0, bytes);
                    }
                    in.close();
                }
            }
            byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
            out.write(endData);
            out.flush();
            out.close();
            // 读取返回数据
            StringBuilder strBuf = new StringBuilder();
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line = null;
            while ((line = reader.readLine()) != null) {
                strBuf.append(line);
            }
            res = strBuf.toString();
            reader.close();
            reader = null;
        } catch (Exception e) {
            logger.error("[HttpsUtls][formUpload] is Exception. {}", e);
        } finally {
            if (conn != null) {
                conn.disconnect();
                conn = null;
            }
            if (in != null) {
                try {
                    in.close();
                } catch (Exception e) {
                    logger.error("[HttpsUtls][formUpload] in.close is Exception. {}", e);
                }
            }
        }
        logger.info("\n【响应数据】: {} ", res);
        return res;
    }
    /**
     * * 通过url下载单个文件
     * 
     * @param downloadUrl
     * @param urlParams
     * @param maxSize
     * @param folder
     * @param filePath
     * @return
     */
    public static String downloadFile(String serverUrl, Map<String, String> params, String folder, String filePath) {
        logger.info("\n【请求地址】: {} \n【请求参数】: {} ", serverUrl, params);
        HttpURLConnection conn = null;
        InputStream in = null;
        FileOutputStream os = null;
        try {
            // 构建请求参数
            StringBuilder sbParam = new StringBuilder(serverUrl);
            if (mapIsNotEmpty(params)) {
                sbParam.append("?");
                if (mapIsNotEmpty(params)) {
                    for (Map.Entry<String, String> entry : params.entrySet()) {
                        sbParam.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
                        sbParam.append("=");
                        sbParam.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
                        sbParam.append("&");
                    }
                }
            }
            // 发送请求
            URL url = new URL(sbParam.toString());
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            conn.setRequestMethod(GET);
            conn.setUseCaches(false);
            conn.setDoOutput(true);
            conn.setReadTimeout(TIMEOUT);
            conn.setConnectTimeout(TIMEOUT);
            int responseCode = conn.getResponseCode();
            if (responseCode != HttpURLConnection.HTTP_OK) {
                logger.error("[HttpsUtls][downloadFile] responseCode:{}", responseCode);
                return StringUtils.EMPTY;
            }
            boolean resultIsFile = parseIsFile(conn);
            // 返回不是文件流,则返回字符串
            if (!resultIsFile) {
                // 读取返回数据
                StringBuilder strBuf = new StringBuilder();
                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line = null;
                while ((line = reader.readLine()) != null) {
                    strBuf.append(line);
                }
                reader.close();
                reader = null;
                logger.info("\n【响应数据】: {} ", strBuf);
                return strBuf.toString();
            }
            // 返回文件流
            File targetFile = new File(folder, filePath);
            in = conn.getInputStream();
            os = new FileOutputStream(targetFile);
            byte[] buffer = new byte[4 * 1024];
            int read;
            while ((read = in.read(buffer)) > -1) {
                os.write(buffer, 0, read);
            }
            os.flush();
            return filePath;
        } catch (Exception e) {
            logger.error("[HttpsUtls][downloadFile] Exception", e);
            return StringUtils.EMPTY;
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (Exception e) {
                    logger.error("[HttpsUtls] [downloadFile] in.close is exception. {}", e);
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (Exception e) {
                    logger.error("[HttpsUtls] [downloadFile] os.close is exception. {}", e);
                }
            }
            if (conn != null) {
                try {
                    conn.disconnect();
                } catch (Exception e) {
                    logger.error("[HttpsUtls] [downloadFile] conn.disconnect is exception. {}", e);
                }
            }
        }
    }
    // 通用
    // /
    /**
     * * 是否是文件
     * 
     * @param conn
     * @return
     */
    private static boolean parseIsFile(HttpURLConnection conn) {
        String isFile = conn.getHeaderField("Content-Disposition");
        if (StringUtils.isEmpty(isFile)) {
            return false;
        }
        return StringUtils.contains(isFile, RESULT_FILE);
    }
    /**
     * 判断Map是否不为空
     * 
     * @param m
     * @return
     */
    public static boolean mapIsNotEmpty(Map<?, ?> m) {
        return !mapIsEmpty(m);
    }
    /**
     * 判断Map是否为空
     * 
     * @param m
     * @return
     */
    public static boolean mapIsEmpty(Map<?, ?> m) {
        return m == null || m.isEmpty();
    }
    /**
     * 测试http地址是否通畅  
     * @param url
     * @return
     */
    public static boolean isOk(String url) {
        if(StringUtils.isEmpty(url)) return false;
        try {
            URL netUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) netUrl.openConnection();
            connection.setConnectTimeout(3000); //连接主机超时时间ms
            connection.setReadTimeout(3000); //从主机读取数据超时时间ms
            if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) {
                System.out.println("网络联通!");
                return true;
            }
        } catch (IOException e) {
            logger.error("连接不通", e.getMessage());
            return false;
        }
        return false;
    }
}


相关文章
|
1天前
|
前端开发 网络协议 安全
【网络原理】——HTTP协议、fiddler抓包
HTTP超文本传输,HTML,fiddler抓包,URL,urlencode,HTTP首行方法,GET方法,POST方法
|
1天前
|
存储 JSON 缓存
【网络原理】——HTTP请求头中的属性
HTTP请求头,HOST、Content-Agent、Content-Type、User-Agent、Referer、Cookie。
|
3天前
|
JSON Dart 前端开发
鸿蒙应用开发从入门到入行 - 篇7:http网络请求
在本篇文章里,您将掌握鸿蒙开发工具DevEco的基本使用、ArkUI里的基础组件,并通过制作一个简单界面掌握使用
31 8
|
2天前
|
数据采集 安全 搜索推荐
HTTP代理IP纯净度 提升用户网络体验的核心竞争力
随着互联网发展,使用HTTP动态代理IP的需求日益增加。高纯净度的代理IP在隐私与安全、网络体验和业务运营方面至关重要。它能保护用户信息、提高数据安全性、确保访问速度和连接稳定,并提升业务效率与信誉度。
16 2
|
7天前
|
缓存 负载均衡 监控
HTTP代理服务器在网络安全中的重要性
随着科技和互联网的发展,HTTP代理IP中的代理服务器在企业业务中扮演重要角色。其主要作用包括:保护用户信息、访问控制、缓存内容、负载均衡、日志记录和协议转换,从而在网络管理、性能优化和安全性方面发挥关键作用。
27 2
|
10天前
|
SQL 安全 网络安全
网络安全与信息安全:知识分享####
【10月更文挑战第21天】 随着数字化时代的快速发展,网络安全和信息安全已成为个人和企业不可忽视的关键问题。本文将探讨网络安全漏洞、加密技术以及安全意识的重要性,并提供一些实用的建议,帮助读者提高自身的网络安全防护能力。 ####
48 17
|
21天前
|
存储 SQL 安全
网络安全与信息安全:关于网络安全漏洞、加密技术、安全意识等方面的知识分享
随着互联网的普及,网络安全问题日益突出。本文将介绍网络安全的重要性,分析常见的网络安全漏洞及其危害,探讨加密技术在保障网络安全中的作用,并强调提高安全意识的必要性。通过本文的学习,读者将了解网络安全的基本概念和应对策略,提升个人和组织的网络安全防护能力。
|
22天前
|
SQL 安全 网络安全
网络安全与信息安全:关于网络安全漏洞、加密技术、安全意识等方面的知识分享
随着互联网的普及,网络安全问题日益突出。本文将从网络安全漏洞、加密技术和安全意识三个方面进行探讨,旨在提高读者对网络安全的认识和防范能力。通过分析常见的网络安全漏洞,介绍加密技术的基本原理和应用,以及强调安全意识的重要性,帮助读者更好地保护自己的网络信息安全。
43 10
|
23天前
|
SQL 安全 网络安全
网络安全与信息安全:关于网络安全漏洞、加密技术、安全意识等方面的知识分享
在数字化时代,网络安全和信息安全已成为我们生活中不可或缺的一部分。本文将介绍网络安全漏洞、加密技术和安全意识等方面的内容,并提供一些实用的代码示例。通过阅读本文,您将了解到如何保护自己的网络安全,以及如何提高自己的信息安全意识。
47 10
|
23天前
|
存储 监控 安全
云计算与网络安全:云服务、网络安全、信息安全等技术领域的融合与挑战
本文将探讨云计算与网络安全之间的关系,以及它们在云服务、网络安全和信息安全等技术领域中的融合与挑战。我们将分析云计算的优势和风险,以及如何通过网络安全措施来保护数据和应用程序。我们还将讨论如何确保云服务的可用性和可靠性,以及如何处理网络攻击和数据泄露等问题。最后,我们将提供一些关于如何在云计算环境中实现网络安全的建议和最佳实践。