Http请求两种方式

方式一:

/**
 * 发送https请求
 *
 * @param requestUrl 请求地址
 * @param requestMethod 请求方式(GET、POST)
 * @param outputStr 提交的数据
 * @return 返回微信服务器响应的信息
 */
@Deprecated
public String httpsRequest(String requestUrl, String requestMethod, String outputStr) {
    String resContent = null;
    DefaultHttpClient httpClient = new DefaultHttpClient ();
       try {
           // 创建SSLContext对象,并使用我们指定的信任管理器初始化
           TrustManager[] tm = { new MyX509TrustManager() };
           SSLContext sslContext = SSLContext.getInstance("TLS");
           sslContext.init(null, tm, new java.security.SecureRandom());
           // 从上述SSLContext对象中得到SSLSocketFactory对象
           SSLSocketFactory ssf = new org.apache.http.conn.ssl.SSLSocketFactory(sslContext);
           ssf.setHostnameVerifier(new AllowAllHostnameVerifier ());
           httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme ("https", ssf, 443));

           HttpPost httpGet = new HttpPost (requestUrl);
           httpGet.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows XP)");
           httpGet.setHeader("Content-Type", "application/x-www-form-urlencoded");

           httpGet.setEntity(new StringEntity (outputStr, "UTF-8"));

           HttpResponse response = httpClient.execute(httpGet);
           if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
               HttpEntity httpEntity = response.getEntity();
               if (httpEntity != null) {
                   resContent = EntityUtils.toString(httpEntity);// 取出应答字符串
                   resContent = new String(resContent.getBytes("ISO-8859-1"), "UTF-8");
                   httpEntity.consumeContent();
               }
           } else {
               httpGet.abort();
           }
       } catch (Exception e) {
           log.error("连接超时:{}", e);
       } finally {
           httpClient.getConnectionManager().shutdown();
       }
    return resContent;
}

方式二:

/**
  * 请求,只请求一次,不做重试
   *
   * @param url   请求的地址
   * @param data  提交的数据
   * @param connectTimeoutMs 连接超时时间
   * @param readTimeoutMs 读取超时时间
   * @return
   * @throws Exception
   */
  public String requestOnce(String url, String data, int connectTimeoutMs, int readTimeoutMs) throws Exception {
      BasicHttpClientConnectionManager connManager;
      //设置连接管理器,BasicHttpClientConnectionManager对象线程安全,每次只管理一个连接
      connManager = new BasicHttpClientConnectionManager (
              RegistryBuilder.<ConnectionSocketFactory>create()
                      .register("http", PlainConnectionSocketFactory.getSocketFactory())
                      .register("https", SSLConnectionSocketFactory.getSocketFactory())
                      .build(),
              null,
              null,
              null
      );

      //构建客户端
      HttpClient httpClient = HttpClientBuilder.create()
              .setConnectionManager(connManager)
              .build();

      HttpPost httpPost = new HttpPost(url);

      //设置请求和传输超时时间
      RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(readTimeoutMs).setConnectTimeout(connectTimeoutMs).build();
      httpPost.setConfig(requestConfig);

      //设置请求头和请求数据编码格式
      StringEntity postEntity = new StringEntity(data, "UTF-8");
      httpPost.addHeader("Content-Type", "text/xml");
      httpPost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows XP)");
      httpPost.setEntity(postEntity);

      //发送请求,接收请求数据,将请求数据转为string
      HttpResponse httpResponse = httpClient.execute(httpPost);
      HttpEntity httpEntity = httpResponse.getEntity();
      return EntityUtils.toString(httpEntity, "UTF-8");

  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值