/** @return 支付请求URL */
 public static String getBillPayURL() {
   if (BCCache.getInstance().isTestMode) {
     return getRandomHost() + BILL_PAY_SANDBOX_URL;
   } else {
     return getRandomHost() + BILL_PAY_URL;
   }
 }
 /** @return 查询支付订单数目URL */
 public static String getBillsCountQueryURL() {
   if (BCCache.getInstance().isTestMode) {
     return getRandomHost() + BILLS_COUNT_QUERY_SANDBOX_URL;
   } else {
     return getRandomHost() + BILLS_COUNT_QUERY_URL;
   }
 }
  public static Response getPayPalAccessToken() {

    Response response = null;

    HttpsURLConnection httpsURLConnection = null;
    try {
      URL urlObj = new URL(getPayPalAccessTokenUrl());
      httpsURLConnection = (HttpsURLConnection) urlObj.openConnection();
      httpsURLConnection.setConnectTimeout(BCCache.getInstance().connectTimeout);
      httpsURLConnection.setRequestMethod("POST");
      httpsURLConnection.setRequestProperty("Accept", "application/json");
      httpsURLConnection.setRequestProperty(
          "Authorization",
          BCSecurityUtil.getB64Auth(
              BCCache.getInstance().paypalClientID, BCCache.getInstance().paypalSecret));
      httpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      httpsURLConnection.setDoOutput(true);
      httpsURLConnection.setChunkedStreamingMode(0);

      response = writeStream(httpsURLConnection, "grant_type=client_credentials");
      if (response == null) {

        response = readStream(httpsURLConnection);
      }
    } catch (MalformedURLException e) {
      e.printStackTrace();
      response = new Response();
      response.content = e.getMessage();
      response.code = -1;
    } catch (IOException e) {
      e.printStackTrace();
      response = new Response();
      response.content = e.getMessage();
      response.code = -1;
    } finally {
      if (httpsURLConnection != null) httpsURLConnection.disconnect();
    }

    return response;
  }
  /**
   * http post 请求
   *
   * @param url 请求url
   * @param jsonStr post参数
   * @return HttpResponse请求结果实例
   */
  public static Response httpPost(String url, String jsonStr) {
    Response response = null;

    HttpsURLConnection httpsURLConnection = null;
    try {
      URL urlObj = new URL(url);
      httpsURLConnection = (HttpsURLConnection) urlObj.openConnection();
      httpsURLConnection.setRequestMethod("POST");
      httpsURLConnection.setConnectTimeout(BCCache.getInstance().connectTimeout);
      httpsURLConnection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
      httpsURLConnection.setDoOutput(true);
      httpsURLConnection.setChunkedStreamingMode(0);

      // start to post
      response = writeStream(httpsURLConnection, jsonStr);

      if (response == null) { // if post successfully

        response = readStream(httpsURLConnection);
      }
    } catch (MalformedURLException e) {
      e.printStackTrace();

      response = new Response();
      response.content = e.getMessage();
      response.code = -1;
    } catch (IOException e) {
      e.printStackTrace();

      response = new Response();
      response.content = e.getMessage();
      response.code = -1;
    } catch (Exception ex) {
      ex.printStackTrace();
      response = new Response();
      response.content = ex.getMessage();
      response.code = -1;
    } finally {
      if (httpsURLConnection != null) httpsURLConnection.disconnect();
    }

    return response;
  }
  /**
   * http get 请求
   *
   * @param url 请求uri
   * @return HttpResponse请求结果实例
   */
  public static Response httpGet(String url) {

    Response response = null;

    HttpsURLConnection httpsURLConnection = null;
    try {
      URL urlObj = new URL(url);
      httpsURLConnection = (HttpsURLConnection) urlObj.openConnection();
      httpsURLConnection.setConnectTimeout(BCCache.getInstance().connectTimeout);
      httpsURLConnection.setDoInput(true);

      response = readStream(httpsURLConnection);

    } catch (MalformedURLException e) {
      e.printStackTrace();

      response = new Response();
      response.content = e.getMessage();
      response.code = -1;
    } catch (IOException e) {
      e.printStackTrace();

      response = new Response();
      response.content = e.getMessage();
      response.code = -1;
    } catch (Exception ex) {
      ex.printStackTrace();
      response = new Response();
      response.content = ex.getMessage();
      response.code = -1;
    } finally {
      if (httpsURLConnection != null) httpsURLConnection.disconnect();
    }

    return response;
  }
 public static String getNotifyPayResultSandboxUrl() {
   return getRandomHost() + NOTIFY_PAY_RESULT_SANDBOX_URL + "/" + BCCache.getInstance().appId;
 }
 public static String getPayPalAccessTokenUrl() {
   if (BCCache.getInstance().paypalPayType == BCPay.PAYPAL_PAY_TYPE.LIVE)
     return PAYPAL_LIVE_BASE_URL + PAYPAL_ACCESS_TOKEN_URL;
   else return PAYPAL_SANDBOX_BASE_URL + PAYPAL_ACCESS_TOKEN_URL;
 }