当前位置: 首页 > news >正文

自己做网站能赚钱么手机百度下载免费安装

自己做网站能赚钱么,手机百度下载免费安装,珠宝营销型网站设计,wordpress 动态效果在Java中,有多种方式可以实现HTTP或HTTPS请求。以下是使用第三方库Apache HttpClient来实现HTTP/HTTPS请求的工具类。 优势和特点 URIBuilder的优势在于它提供了一种简单而灵活的方式来构造URI,帮助开发人员避免手动拼接URI字符串,并处理参…

在Java中,有多种方式可以实现HTTP或HTTPS请求。以下是使用第三方库Apache HttpClient来实现HTTP/HTTPS请求的工具类。

优势和特点

URIBuilder的优势在于它提供了一种简单而灵活的方式来构造URI,帮助开发人员避免手动拼接URI字符串,并处理参数的编码和转义。此外,URIBuilder还提供了其他一些有用的方法,例如添加路径段、设置查询字符串等。这使得URI的构建过程更加清晰和易于管理。

使用 Apache HttpClient

要使用Apache HttpClient,你首先需要将HttpClient库添加到你的项目中。如果你使用Maven,可以在pom.xml中添加以下依赖:

<dependency> 

    <groupId>org.apache.httpcomponents</groupId> 

    <artifactId>httpclient</artifactId> 

    <version>4.5.13</version> <!-- 使用时请检查最新版本 --> 

</dependency>

然后,你可以使用以下代码示例来发送HTTP请求:

import org.apache.commons.collections.MapUtils;

import org.apache.http.HttpEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.utils.URIBuilder;

import org.apache.http.conn.ssl.NoopHostnameVerifier;

import org.apache.http.conn.ssl.SSLConnectionSocketFactory;

import org.apache.http.conn.ssl.TrustStrategy;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.ssl.SSLContextBuilder;

import org.apache.http.util.EntityUtils;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import javax.net.ssl.HostnameVerifier;

import javax.net.ssl.SSLContext;

import java.io.IOException;

import java.security.KeyManagementException;

import java.security.KeyStoreException;

import java.security.NoSuchAlgorithmException;

import java.security.cert.CertificateException;

import java.security.cert.X509Certificate;

import java.util.HashMap;

import java.util.Map;

public class HttpsUtils {

    private static Logger logger = LoggerFactory.getLogger(HttpsUtils.class);

    static CloseableHttpClient httpClient;

    static CloseableHttpResponse httpResponse;

    public static CloseableHttpClient createSSLClientDefault() {

        try {

            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {

                // 信任所有

                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {

                    return true;

                }

            }).build();

            HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;

            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);

            return HttpClients.custom().setSSLSocketFactory(sslsf).build();

        } catch (KeyManagementException e) {

            e.printStackTrace();

        } catch (NoSuchAlgorithmException e) {

            e.printStackTrace();

        } catch (KeyStoreException e) {

            e.printStackTrace();

        }

        return HttpClients.createDefault();

    }

    /**

    * 发送https||http get请求

    *

    * @throws Exception

    */

    public static String sendByHttpGet(Map<String, Object> params, String url) {

        try {

            URIBuilder uriBuilder = new URIBuilder(url);

            if(MapUtils.isNotEmpty(params)) {

                for (Map.Entry<String, Object> entry : params.entrySet()) {

                    uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());

                }

            }

            HttpGet httpGet = new HttpGet(uriBuilder.build());

            httpGet.addHeader("Content-type", "application/json; charset=utf-8");

            httpGet.setHeader("Accept", "application/json");

            httpClient = HttpsUtils.createSSLClientDefault();

            httpResponse = httpClient.execute(httpGet);

            HttpEntity httpEntity = httpResponse.getEntity();

            if (httpEntity != null) {

                String jsObject = EntityUtils.toString(httpEntity, "UTF-8");

                return jsObject;

            } else {

                return null;

            }

        } catch (Exception e) {

            e.printStackTrace();

            logger.error("description : {{}} ,details : {{}}", "请求异常", e.getStackTrace());

        } finally {

            try {

                httpResponse.close();

                httpClient.close();

            } catch (IOException e) {

                logger.error("description : {{}} ,details : {{}}", "请求流关闭异常", e.getStackTrace());

                e.printStackTrace();

            }

        }

        return null;

    }

    /**

    * 发送https||http get请求

    *

    * @throws Exception

    */

    public static byte[] sendByteByHttpGet(Map<String, Object> params, String url) {

        try {

            URIBuilder uriBuilder = new URIBuilder(url);

            if(MapUtils.isNotEmpty(params)) {

                for (Map.Entry<String, Object> entry : params.entrySet()) {

                    uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());

                }

            }

            HttpGet httpGet = new HttpGet(uriBuilder.build());

            httpGet.addHeader("Content-type", "application/json; charset=utf-8");

            httpGet.setHeader("Accept", "application/json");

            httpClient = HttpsUtils.createSSLClientDefault();

            httpResponse = httpClient.execute(httpGet);

            HttpEntity httpEntity = httpResponse.getEntity();

            if (httpEntity != null) {

                byte[] bytes = EntityUtils.toByteArray(httpEntity);

                return bytes;

            } else {

                return new byte[0];

            }

        } catch (Exception e) {

            e.printStackTrace();

            logger.error("description : {{}} ,details : {{}}", "读取二进制保存的图片异常", e.getStackTrace());

        } finally {

            try {

                httpResponse.close();

                httpClient.close();

            } catch (IOException e) {

                e.printStackTrace();

                logger.error("description : {{}} ,details : {{}}", "读取二进制保存的图片关闭流时异常", e.getStackTrace());

            }

        }

        return new byte[0];

    }

}

http://www.hengruixuexiao.com/news/24464.html

相关文章:

  • 西宁专业做网站的经典软文文案
  • c 网站开发项目江西省seo
  • 网站建设及网络营销百度搜索优化软件
  • 重庆沙坪坝企业网站建设联系电话设计网站用什么软件
  • 网站制作书生设计培训学院
  • 深圳网站做的好的公司名称哪些平台可以免费发布产品
  • 赣州北京网站建设巨量引擎app
  • 无为做网站站长工具国色天香
  • 做论坛和做网站有什么区别什么叫seo优化
  • 做品牌网站找谁我想开个网站平台怎么开呢
  • 风水网站开发seo优化关键词放多少合适
  • 做网站需要招什么条件交换链接的作用
  • jsp项目个人网站开发济南网站运营公司
  • 网站生成静态天津海外seo
  • 网站建设新手百度推广官方网站登录入口
  • 源码猫网站建设ym361百度官网首页网址
  • 电影资源网站建设公司做网页要多少钱
  • 网站设计的毕业设计青岛网页搜索排名提升
  • 网站建设方案ppt 枫子科技在线视频用什么网址
  • 自己做竞猜网站挣钱吗广告推广接单平台
  • 白城网站开发nba最新新闻消息
  • 如何做网站安全加固厦门网站建设公司
  • 重庆重大新闻事件优化关键词快速排名
  • 呼和浩特建设工程安全管理网站个人建站
  • 白云区专业网站建设百度商城官网首页
  • 想在网上做开发网站接活儿百度指数下载手机版
  • 做网站前后端的发布流程百度竞价排名平台
  • 凡客网址百度seo按天计费
  • 微信连接微网站吗百度竞价sem入门教程
  • wordpress ts cdseo网站权重