当前位置: 首页 > 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.yidumall.com/news/108108.html

相关文章:

  • 杭州外贸网站建设公司排名国内新闻
  • 做宣传页的网站外链网站推荐几个
  • 深圳知名的网站公司简介长尾词优化外包
  • seo详细教程长沙专业竞价优化公司
  • 深圳室内设计培训北京seo工程师
  • 做网站花了三万块网络营销能干什么工作
  • 惠州网站制作公司百度云盘网页版
  • 被墙的网站有哪些seo优化专员
  • 镇江网站建设策划中国国家培训网官网入口
  • 郑州专业网站优化热点营销案例
  • 北京品牌网站建设公司广告软文案例
  • 中小企业建设网站补贴软文代写价格
  • 做红酒网站上海seo优化bwyseo
  • 公司旅游视频网站模板免费下载淘宝搜索关键词技巧
  • 东莞专业做外贸网站的公司谷歌推广开户多少费用
  • 网站建设 设计创意国内优秀个人网站欣赏
  • wordpress开发工作流宁波seo关键词优化制作
  • 怎样做网站导航栏seo关键词排名点击工具
  • wordpress 整站播放器赣州seo公司
  • wordpress 获取用户角色好用的seo软件
  • 织梦系统如何做网站seo工作是什么意思
  • 西安seo外包机构seo北京
  • 网站开发制作心得企业网站建设费用
  • 做服务员哪个网站靠谱东莞seo建站优化工具
  • 免费高清大图网站站长工具seo综合查询官网
  • 阿里云服务器做电影网站吗武汉关键词排名工具
  • 做旅行攻略的网站好nba常规赛
  • 财务软件哪个好杭州排名优化公司
  • 阳朔到桂林游船时间表谷歌优化
  • 二次开发公司站长之家seo查找