当前位置: 首页>>代码示例>>Java>>正文


Java HttpsURLConnection.setUseCaches方法代码示例

本文整理汇总了Java中javax.net.ssl.HttpsURLConnection.setUseCaches方法的典型用法代码示例。如果您正苦于以下问题:Java HttpsURLConnection.setUseCaches方法的具体用法?Java HttpsURLConnection.setUseCaches怎么用?Java HttpsURLConnection.setUseCaches使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.net.ssl.HttpsURLConnection的用法示例。


在下文中一共展示了HttpsURLConnection.setUseCaches方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: reSend

import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
 * 重发
 * 
 * @param urlStr
 * @param parameters
 * @param count
 */
private static void reSend(String urlStr, String parameters, Map<String, Integer> count) throws IOException {
	if (count.get("times") == null) {
		count.put("times", 0);
	}
	int times = count.get("times");
	if (times < 5) {
		URL url = new URL(urlStr);
		HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
		conn.setDoOutput(true);
		conn.setDoInput(true);
		conn.setRequestMethod("POST");
		conn.setUseCaches(false);
		conn.setReadTimeout(3000);
		conn.setConnectTimeout(3000);

		OutputStream output = conn.getOutputStream();
		output.write(parameters.getBytes("utf-8"));
		output.flush();

		BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
		String s = null;
		StringBuilder sb = new StringBuilder();
		while ((s = reader.readLine()) != null) {
			sb.append(s);
		}
		reader.close();
		JSONObject jsonObject = JSONObject.parseObject(sb.toString());
		String errcode = jsonObject.get("errcode").toString();
		if (!errcode.equals("0")) {
			count.put("times", count.get("times") + 1);
			reSend(urlStr, parameters, count);
		}
	}
}
 
开发者ID:iBase4J,项目名称:iBase4J-Common,代码行数:42,代码来源:WeiXinCompanySendMsg.java

示例2: begin

import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
void begin() throws IOException {
  try {
    _conn = (HttpsURLConnection) _url.openConnection();
    _conn.setRequestMethod("POST");
    for (Map.Entry<String, String> entry : _config.header.entrySet()) {
      _conn.setRequestProperty(entry.getKey(), entry.getValue());
    }
    _conn.setUseCaches(false);
    _conn.setDoInput(false);
    _conn.setDoOutput(true);
    _conn.connect();

    _writer = new BufferedWriter(new OutputStreamWriter(_conn.getOutputStream()));
    _writer.append("{\"app\":\"").append(_config.app)
        .append("\",\"version\":\"").append(_config.version)
        .append("\",\"relay_app_id\":\"").append(_config.relayAppId);

    if (_envInfo.device != null) {
      _writer.append("\",\"device\":\"").append(_envInfo.device);
    }

    if (_envInfo.getAppUsedMemory() > 0) {
      _writer.append("\",\"app_mem_used\":\"").append(Long.toString(_envInfo.getAppUsedMemory()));
    }

    if (_envInfo.getDeviceFreeMemory() > 0) {
      _writer.append("\",\"device_mem_free\":\"").append(Long.toString(_envInfo.getDeviceFreeMemory()));
    }

    if (_envInfo.getDeviceTotalMemory() > 0) {
      _writer.append("\",\"device_mem_total\":\"").append(Long.toString(_envInfo.getDeviceTotalMemory()));
    }

    if (_envInfo.getBatteryLevel() > 0) {
      _writer.append("\",\"battery_level\":\"").append(Float.toString(_envInfo.getBatteryLevel()));
    }

    if (_envInfo.getCountry() != null) {
      _writer.append("\",\"country\":\"").append(_envInfo.getCountry());
    }

    if (_envInfo.getRegion() != null) {
      _writer.append("\",\"region\":\"").append(_envInfo.getRegion());
    }

    if (_envInfo.network != null) {
      _writer.append("\",\"network\":\"").append(_envInfo.network);
    }

    if (_envInfo.osName != null) {
      _writer.append("\",\"os\":\"").append(_envInfo.osName);
    }

    if (_envInfo.osVersion != null) {
      _writer.append("\",\"os_version\":\"").append(_envInfo.osVersion);
    }

    _writer.append("\",\"measurements\":[");
    _measurements = 0;

  } catch (Exception e) {
    if (_config.debug) {
      Log.d(TAG, e.getMessage());
    }
    disconnect();
    if (e instanceof IOException) {
      throw e;
    }
  }
}
 
开发者ID:rakutentech,项目名称:android-perftracking,代码行数:71,代码来源:EventWriter.java

示例3: createRequest

import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
   * 创建Http/Https请求对象
   * @author Rocye
   * @param url 请求地址
   * @param method 请求方式:GET/POST
   * @param certPath 证书路径
   * @param certPass 证书密码
* @param useCert 是否需要证书
   * @return Https连接
   * @throws Exception 任何异常
   * @version 2017.11.14
   */
  private HttpsURLConnection createRequest(String url, String method, String certPath, String certPass, boolean useCert) throws Exception{
      URL realUrl = new URL(url);
      HttpsURLConnection connection = (HttpsURLConnection)realUrl.openConnection();

      //设置证书
if(useCert){
	KeyStore clientStore = KeyStore.getInstance("PKCS12");
	InputStream inputStream = new FileInputStream(certPath);
	clientStore.load(inputStream, certPass.toCharArray());
	KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
	kmf.init(clientStore, certPass.toCharArray());
	KeyManager[] kms = kmf.getKeyManagers();
	SSLContext sslContext = SSLContext.getInstance("TLSv1");
	sslContext.init(kms, null, new SecureRandom());
	connection.setSSLSocketFactory(sslContext.getSocketFactory());
}

      // 设置通用的请求属性
      connection.setRequestProperty("Accept", "*/*");
      connection.setRequestProperty("Connection", "Keep-Alive");
      connection.setConnectTimeout(this.connectTimeout);
      connection.setReadTimeout(this.readTimeout);
      if("POST".equals(method)){
          // 发送POST请求必须设置如下两行
          connection.setDoOutput(true);
          connection.setDoInput(true);
          connection.setUseCaches(false);   // 忽略缓存
          connection.setRequestMethod("POST");
      }
      return connection;
  }
 
开发者ID:rocye,项目名称:wx-idk,代码行数:44,代码来源:HttpsRequestTools.java

示例4: HttpsGet

import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
 *
 * @param requestURL
 * @param queryString
 * @throws MalformedURLException
 * @throws IOException
 */
public HttpsGet(String requestURL, String queryString) throws MalformedURLException, IOException {
    URL url = new URL(requestURL + "?" + queryString);
    connection = (HttpsURLConnection) url.openConnection();
    connection.setUseCaches(false);
    connection.setRequestMethod("GET");
    connection.setDoInput(true);
}
 
开发者ID:B-V-R,项目名称:VirusTotal-public-and-private-API-2.0-implementation-in-pure-Java,代码行数:15,代码来源:HttpsGet.java

示例5: HttpsPost

import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
 *
 * @param requestURL
 * @param entity
 * @throws MalformedURLException
 * @throws IOException
 */
public HttpsPost(String requestURL, MultipartEntity entity) throws MalformedURLException, IOException {
    String boundary = "e2a540ab4e6c5ed79c01157c255a2b5007e157d7";
    URL url = new URL(requestURL);
    connection = (HttpsURLConnection) url.openConnection();
    connection.setUseCaches(false);
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    connection.setDoInput(true);
    connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
    process(entity, connection);
}
 
开发者ID:B-V-R,项目名称:VirusTotal-public-and-private-API-2.0-implementation-in-pure-Java,代码行数:19,代码来源:HttpsPost.java

示例6: httpsRequest

import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
     * 发起https请求并获取结果
     * 
     * @param requestUrl
     *            请求地址
     * @param requestMethod
     *            请求方式(GET、POST)
     * @param outputStr
     *            提交的数据
     * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
     */
    public static JSONObject httpsRequest(String requestUrl,String requestMethod,String outputStr){
        JSONObject jsonObject = null;
        StringBuffer buffer = new StringBuffer();
        try {
            // 创建SSLContext对象,并使用我们指定的信任管理器初始化
            TrustManager[] tm = { new SaicX509TrustManager() };
//          SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
            SSLContext sslContext = SSLContext.getInstance("TLS", "SunJSSE");
            sslContext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocketFactory对象
            SSLSocketFactory ssf = sslContext.getSocketFactory();

            URL url = new URL(requestUrl);
            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
            httpUrlConn.setSSLSocketFactory(ssf);

            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置请求方式(GET/POST)
            httpUrlConn.setRequestMethod(requestMethod);

            if ("GET".equalsIgnoreCase(requestMethod))
                httpUrlConn.connect();

            // 当有数据需要提交时
            if (null != outputStr) {
                OutputStream outputStream = httpUrlConn.getOutputStream();
                // 注意编码格式,防止中文乱码
                outputStream.write(outputStr.getBytes("UTF-8"));
                outputStream.close();
            }

            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();
            jsonObject = JSONObject.fromObject(buffer.toString());
        } catch (ConnectException ce) {
            logger.error("connection timed out cause by " + ce.getMessage());
        } catch (Exception e) {
            logger.error("https request error : " + e.getMessage());
        }
        return jsonObject;
    }
 
开发者ID:tojaoomy,项目名称:private-WeChat,代码行数:68,代码来源:CommonUtil.java

示例7: httpsRequest

import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
 * @param requestUrl
 * @param requestMethod
 * @param outputStr
 * @return
 */
public static JSONObject httpsRequest(String requestUrl, String requestMethod, String outputStr) {
    JSONObject jsonObject = null;
    StringBuffer buffer = new StringBuffer();
    try {
        TrustManager[] tm = {new MyX509TrustManager()};
        SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
        sslContext.init(null, tm, new java.security.SecureRandom());
        SSLSocketFactory ssf = sslContext.getSocketFactory();
        URL url = new URL(requestUrl);
        HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
        httpUrlConn.setSSLSocketFactory(ssf);
        httpUrlConn.setDoOutput(true);
        httpUrlConn.setDoInput(true);
        httpUrlConn.setUseCaches(false);
        httpUrlConn.setRequestMethod(requestMethod);
        if ("GET".equalsIgnoreCase(requestMethod))
            httpUrlConn.connect();
        if (null != outputStr) {
            OutputStream outputStream = httpUrlConn.getOutputStream();
            outputStream.write(outputStr.getBytes("UTF-8"));
            outputStream.close();
        }
        InputStream inputStream = httpUrlConn.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String str = null;
        while ((str = bufferedReader.readLine()) != null) {
            buffer.append(str);
        }
        bufferedReader.close();
        inputStreamReader.close();
        inputStream.close();
        inputStream = null;
        httpUrlConn.disconnect();
        jsonObject = JSONObject.parseObject(buffer.toString());
    } catch (ConnectException ce) {
        ce.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return jsonObject;
}
 
开发者ID:Evan1120,项目名称:wechat-api-java,代码行数:49,代码来源:WeixinUtil.java

示例8: httpsRequest

import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
public static JSONObject httpsRequest(String requestUrl, String requestMethod, String outputStr) {
	JSONObject jsonObject = null;
	try {
		// 创建SSLContext对象,并使用我们指定的信任管理器初始化
		TrustManager[] tm = { new HttpsX509TrustManager() };
		SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
		sslContext.init(null, tm, new java.security.SecureRandom());
		// 从上述SSLContext对象中得到SSLSocketFactory对象
		SSLSocketFactory ssf = sslContext.getSocketFactory();
		URL url = new URL(requestUrl);
		HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
		conn.setSSLSocketFactory(ssf);
		conn.setDoOutput(true);
		conn.setDoInput(true);
		conn.setRequestProperty("Connection", "keep-alive");
		conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36");
		conn.setUseCaches(false);
		conn.setRequestProperty("Content-Type", "application/json;;charset=UTF-8");
		// 设置请求方式(GET/POST)
		conn.setRequestMethod(requestMethod);
		// 当outputStr不为null时向输出流写数据
		if (null != outputStr) {
			OutputStream outputStream = conn.getOutputStream();
			// 注意编码格式
			outputStream.write(outputStr.getBytes("UTF-8"));
			outputStream.close();
		}
		// 从输入流读取返回内容
		InputStream inputStream = conn.getInputStream();
		InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
		BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
		String str = null;
		StringBuffer buffer = new StringBuffer();
		while ((str = bufferedReader.readLine()) != null) {
			buffer.append(str);
		}
		// 释放资源
		bufferedReader.close();
		inputStreamReader.close();
		inputStream.close();
		inputStream = null;
		conn.disconnect();
		jsonObject = JSONObject.parseObject(buffer.toString());
	} catch (ConnectException ce) {
		ce.printStackTrace();
	} catch (Exception e) {
		e.printStackTrace();
	}
	return jsonObject;
}
 
开发者ID:Fetax,项目名称:Fetax-AI,代码行数:51,代码来源:HttpUtil.java


注:本文中的javax.net.ssl.HttpsURLConnection.setUseCaches方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。