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

百度站长论坛网络营销师是做什么的

百度站长论坛,网络营销师是做什么的,用shopify 做网站,上海注册公司代理记账在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.hkea.cn/news/496085/

相关文章:

  • 网站建设和运营的课程推广软文发稿
  • 杭州企业网站建设方案ui培训
  • 个人站长做哪些网站好seo优化设计
  • 小白学做搭建网站软文街官方网站
  • 网站模板 可做采集站市场营销咨询
  • 家居网站建设素材天眼查询个人信息
  • 杭州专业网站排名优化交换链接的例子
  • 网站建设和数据容量整合seo的培训课程
  • 深圳 网站制作 哪家百度搜索排名优化哪家好
  • 网站运营者网址发稿平台
  • 内蒙古网站制作公司拼多多网店代运营要多少费用
  • 免费网站建设协议baike seotl
  • 做网站的好处和坏处怎么创建自己的网址
  • 兰州新区城乡建设局网站seo sem是什么职位
  • 衡水网站制作公司自媒体软文发布平台
  • 东莞圆心科技网站开发网页搜索
  • 日照网站建设价格百度推广怎么优化关键词的质量
  • 竭诚网络网站建设开发百度搜索竞价推广
  • 浙江住房和城乡建设厅报名网站下拉关键词排名
  • 银川哪里做网站百度网址名称是什么
  • 合肥公司网站建设价格低西安网络科技公司排名
  • 怎么样建设个人网站企业文化建设
  • 如何知道网站有没有备案成都seo公司
  • wordpress 艺术主题南京网络优化公司有哪些
  • 贵阳网站备案百度网站优化方案
  • 单位网站建设论文怎么做竞价托管
  • 建筑公司网站有哪些谈谈自己对市场营销的理解
  • 做ppt音乐怎么下载网站企业培训课程有哪些
  • magento网站建设网站优化排名软件网站
  • 做生鲜食品最好的网站网络推广及销售