本文整理汇总了Java中javax.net.ssl.HttpsURLConnection.setConnectTimeout方法的典型用法代码示例。如果您正苦于以下问题:Java HttpsURLConnection.setConnectTimeout方法的具体用法?Java HttpsURLConnection.setConnectTimeout怎么用?Java HttpsURLConnection.setConnectTimeout使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.net.ssl.HttpsURLConnection
的用法示例。
在下文中一共展示了HttpsURLConnection.setConnectTimeout方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: get
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
public static String get(String requestUrl, CookieManager cookies) throws Exception {
URL url = new URL(urlEncode(requestUrl));
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
if (cookies != null)
applyCookies(connection);
connection.setConnectTimeout(8 * 1000);
connection.setRequestProperty("User-Agent", FrameworkConstants.FRAMEWORK_NAME);
connection.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String text;
StringBuilder result = new StringBuilder();
while ((text = in.readLine()) != null)
result.append(text);
in.close();
storeCookies(connection);
return result.toString();
}
示例2: 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);
}
}
}
示例3: buildSSLConn
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
public HttpURLConnection buildSSLConn(String url)throws Exception {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
URL console = new URL(url);
HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
conn.setSSLSocketFactory(sc.getSocketFactory());
conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
conn.setConnectTimeout(connectTimeOut);
conn.setReadTimeout(readTimeOut);
return conn;
}
示例4: 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;
}
示例5: get
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
* 鍙戦�丟et璇锋眰
* @param url
* @return
* @throws NoSuchProviderException
* @throws NoSuchAlgorithmException
* @throws IOException
* @throws KeyManagementException
*/
public static String get(String url,Boolean https) throws NoSuchAlgorithmException, NoSuchProviderException, IOException, KeyManagementException {
StringBuffer bufferRes = null;
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 浠庝笂杩癝SLContext瀵硅薄涓緱鍒癝SLSocketFactory瀵硅薄
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL urlGet = new URL(url);
HttpsURLConnection http = (HttpsURLConnection) urlGet.openConnection();
// 杩炴帴瓒呮椂
http.setConnectTimeout(25000);
// 璇诲彇瓒呮椂 --鏈嶅姟鍣ㄥ搷搴旀瘮杈冩參锛屽澶ф椂闂�
http.setReadTimeout(25000);
http.setRequestMethod("GET");
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setSSLSocketFactory(ssf);
http.setHostnameVerifier(new Verifier());
http.setDoOutput(true);
http.setDoInput(true);
http.connect();
InputStream in = http.getInputStream();
BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));
String valueString = null;
bufferRes = new StringBuffer();
while ((valueString = read.readLine()) != null){
bufferRes.append(valueString);
}
in.close();
if (http != null) {
// 鍏抽棴杩炴帴
http.disconnect();
}
return bufferRes.toString();
}
示例6: post
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
* 鍙戦�丳ost璇锋眰
* @param url
* @param params
* @return
* @throws IOException
* @throws NoSuchProviderException
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public static String post(String url, String params,Boolean https) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {
StringBuffer bufferRes = null;
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 浠庝笂杩癝SLContext瀵硅薄涓緱鍒癝SLSocketFactory瀵硅薄
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL urlGet = new URL(url);
HttpsURLConnection http = (HttpsURLConnection) urlGet.openConnection();
// 杩炴帴瓒呮椂
http.setConnectTimeout(50000);
// 璇诲彇瓒呮椂 --鏈嶅姟鍣ㄥ搷搴旀瘮杈冩參锛屽澶ф椂闂�
http.setReadTimeout(50000);
http.setRequestMethod("POST");
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setSSLSocketFactory(ssf);
http.setHostnameVerifier(new Verifier());
http.setDoOutput(true);
http.setDoInput(true);
http.connect();
OutputStream out = http.getOutputStream();
out.write(params.getBytes("UTF-8"));
out.flush();
out.close();
InputStream in = http.getInputStream();
BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));
String valueString = null;
bufferRes = new StringBuffer();
while ((valueString = read.readLine()) != null){
bufferRes.append(valueString);
}
in.close();
if (http != null) {
// 鍏抽棴杩炴帴
http.disconnect();
}
return bufferRes.toString();
}
示例7: retreiveFlashesNonAsync
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
private boolean retreiveFlashesNonAsync(Context context) throws IOException, JSONException, ParseException{
if (!serverLocation.getProtocol().equals("https")) {
throw new SecurityException("Flashes may only be retrieved over a secure HTTPS connection!");
}
sendDebugNotification("Retrieving flashes from " + serverLocation, context);
HttpsURLConnection urlConnection = (HttpsURLConnection) serverLocation.openConnection();
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(150000);
urlConnection.setConnectTimeout(15000);
urlConnection.connect();
if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream()), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
String data = sb.toString();
JSONObject jsonData = new JSONObject(data);
serverName = jsonData.getString("server");
flashes = convertJsonToFlashes(jsonData.getJSONArray("latest"));
}
sendDebugNotification("Flashes found: " + flashes.length, context);
return true;
}