本文整理汇总了Java中javax.net.ssl.HttpsURLConnection.getInputStream方法的典型用法代码示例。如果您正苦于以下问题:Java HttpsURLConnection.getInputStream方法的具体用法?Java HttpsURLConnection.getInputStream怎么用?Java HttpsURLConnection.getInputStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.net.ssl.HttpsURLConnection
的用法示例。
在下文中一共展示了HttpsURLConnection.getInputStream方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: testExcludedCiphers
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
* Test that verifies that excluded ciphers (SSL_RSA_WITH_RC4_128_SHA,
* TLS_ECDH_ECDSA_WITH_RC4_128_SHA,TLS_ECDH_RSA_WITH_RC4_128_SHA,
* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,TLS_ECDHE_RSA_WITH_RC4_128_SHA) are not
* available for negotiation during SSL connection.
*/
@Test
public void testExcludedCiphers() throws Exception {
URL url = new URL(baseUrl, "/echo?a=b&c=d");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
SSLSocketFactory sslSocketF = clientSslFactory.createSSLSocketFactory();
PrefferedCipherSSLSocketFactory testPreferredCipherSSLSocketF
= new PrefferedCipherSSLSocketFactory(sslSocketF,
excludeCiphers.split(","));
conn.setSSLSocketFactory(testPreferredCipherSSLSocketF);
assertFalse("excludedCipher list is empty", excludeCiphers.isEmpty());
try {
InputStream in = conn.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.copyBytes(in, out, 1024);
fail("No Ciphers in common, SSLHandshake must fail.");
} catch (SSLHandshakeException ex) {
LOG.info("No Ciphers in common, expected succesful test result.", ex);
}
}
示例4: HttpResponse
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
public HttpResponse(HttpsURLConnection httpsURLConnection) throws StarlingBankRequestException {
this.httpsURLConnection = httpsURLConnection;
try {
if (httpsURLConnection.getResponseCode() < HttpsURLConnection.HTTP_BAD_REQUEST){
this.is = httpsURLConnection.getInputStream();
}else {
this.is = httpsURLConnection.getErrorStream();
processStatusCode(httpsURLConnection);
}
this.statusCode = httpsURLConnection.getResponseCode();
this.expiration = httpsURLConnection.getExpiration();
this.request = httpsURLConnection.getURL();
this.expiration = httpsURLConnection.getExpiration();
this.lastModified = httpsURLConnection.getLastModified();
this.responseHeaders = httpsURLConnection.getHeaderFields();
this.contentType = httpsURLConnection.getContentType();
this.contentEncoding = httpsURLConnection.getContentEncoding();
} catch (IOException e) {
e.printStackTrace();
}
}
示例5: getUserInfo
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
public static String getUserInfo(String openId) {
String token = WeiXinUtils.getToken();
if (token != null) {
String urlString = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + token + "&openid="
+ openId;
try {
URL url = new URL(urlString);
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setDoInput(true);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(httpsURLConnection.getInputStream()));
String line = null;
StringBuilder stringBuilder = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
String kfString = stringBuilder.toString();
return kfString;
} catch (Exception ex) {
ex.printStackTrace();
}
}
return null;
}
示例6: fetchToken
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
String fetchToken() {
try {
URL obj = new URL(url);
HttpsURLConnection connection = (HttpsURLConnection) obj.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Authorization", "Basic ZlR1SWpXV1RUZkpHSlNaajBHdDZKTXQ3cXc0YTptTHhoNWpCVWdsTldWb3NqeXpjZjhTYjBKNGNh");
connection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes("grant_type=client_credentials&scope=device_123");
wr.flush();
wr.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
示例7: doClient
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
static void doClient(BadAuthProxyServer server) throws IOException {
// url doesn't matter since we will never make the connection
URL url = new URL("https://anythingwilldo/");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(
new Proxy(Proxy.Type.HTTP,
new InetSocketAddress("localhost", server.getPort())));
try (InputStream is = conn.getInputStream()) {
} catch(IOException unused) {
// no real server, IOException is expected.
// failure if StackOverflowError
} finally {
server.done();
}
}
示例8: print_content
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
private void print_content(HttpsURLConnection con){
ObjectMapper mapper = new ObjectMapper();
if(con!=null){
try {
System.out.println("****** Content of the URL ********");
BufferedReader br =
new BufferedReader(
new InputStreamReader(con.getInputStream()));
String input;
while ((input = br.readLine()) != null){
if(!input.matches("Content")) {
WrapperCb w = mapper.readValue(input, WrapperCb.class);
System.out.println(w.getData().getRates().getUsd());
}
System.out.println(input);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
示例9: 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();
}
示例10: initToken
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
private static void initToken() {
if (tokenTime == null || tokenExpire == null || System.currentTimeMillis() - tokenTime >= tokenExpire) {
String uriString = "https://api.weixin.qq.com/cgi-bin/token?grant_type=" + grantType + "&appid="
+ PropertiesUtil.getString("WX_PUBLIC_APPID") + "&secret="
+ PropertiesUtil.getString("WX_PUBLIC_SECRET");
try {
URL url = new URL(uriString);
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
InputStreamReader inputStreamReader = new InputStreamReader(httpsURLConnection.getInputStream());
int responseInt = inputStreamReader.read();
StringBuffer stringBuffer = new StringBuffer();
while (responseInt != -1) {
stringBuffer.append((char) responseInt);
responseInt = inputStreamReader.read();
}
String tokenString = stringBuffer.toString();
JSONObject jsonObject = JSON.parseObject(tokenString);
if (jsonObject.containsKey("access_token")) {
tokenTime = System.currentTimeMillis();
token = jsonObject.getString("access_token");
tokenExpire = jsonObject.getLong("expires_in");
} else {
// TODO 验证错误
System.out.println(jsonObject.get("errcode"));
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
示例11: verify
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
public static boolean verify(String gRecaptchaResponse){
if (gRecaptchaResponse == null || "".equals(gRecaptchaResponse)) {
return false;
}
try{
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String postParams = "secret=" + secret + "&response="
+ gRecaptchaResponse;
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(postParams);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
JSONObject object = (JSONObject)JSONValue.parse(new StringReader(response.toString()));
return (Boolean)object.get("success");
}catch(Exception e){
e.printStackTrace();
return false;
}
}
示例12: 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();
}
示例13: 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;
}
示例14: insertKfAccount
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
* 添加客服帐号
*
* @param keFu
* @return
*/
public static boolean insertKfAccount(KeFu keFu) {
boolean isOk = false;
String token = WeiXinUtils.getToken();
if (token != null) {
String urlString = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token=" + token;
try {
URL url = new URL(urlString);
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
String kfAccountString = JSONObject.toJSONString(keFu);
httpsURLConnection.setRequestProperty("Content-length", String.valueOf(kfAccountString.length()));
httpsURLConnection.setRequestProperty("Content-Type", "application/json");
httpsURLConnection.setDoOutput(true);
httpsURLConnection.setDoInput(true);
DataOutputStream dataOutputStream = new DataOutputStream(httpsURLConnection.getOutputStream());
dataOutputStream.write(kfAccountString.getBytes());
dataOutputStream.flush();
dataOutputStream.close();
DataInputStream dataInputStream = new DataInputStream(httpsURLConnection.getInputStream());
StringBuffer stringBuffer = new StringBuffer();
int inputByte = dataInputStream.read();
while (inputByte != -1) {
stringBuffer.append((char) inputByte);
inputByte = dataInputStream.read();
}
String kfString = stringBuffer.toString();
JSONObject jsonObject = JSON.parseObject(kfString);
if (jsonObject.containsKey("errcode")) {
int errcode = jsonObject.getIntValue("errcode");
if (errcode == 0) {
isOk = true;
} else {
//TODO 添加客服账号失败
isOk = false;
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return isOk;
}
示例15: upload
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
public static String upload(String fileName, File file) throws IOException {
String urlStr = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=" + WeiXinCompanyUtils.getToken()
+ "&type=file";
// 定义数据分隔符
String boundary = "------------7da2e536604c8";
URL uploadUrl = new URL(urlStr);
HttpsURLConnection uploadConn = (HttpsURLConnection) uploadUrl.openConnection();
uploadConn.setDoOutput(true);
uploadConn.setDoInput(true);
uploadConn.setRequestMethod("POST");
// 设置请求头Content-Type
uploadConn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
// 获取媒体文件上传的输出流(往微信服务器写数据)
OutputStream outputStream = uploadConn.getOutputStream();
// 从请求头中获取内容类型
String contentType = "text";
// 根据内容类型判断文件扩展名
@SuppressWarnings("unused")
String[] f = fileName.split("\\.");
// 请求体开始
outputStream.write(("--" + boundary + "\r\n").getBytes());
// String aaa = String.format("Content-Disposition: form-data;
// name=\"media\"; filename=\""+f[0]+"."+"%s\"\r\n", f[1]);
String aaa = "Content-Disposition: form-data; name=\"media\"; filename=\"" + fileName + "\"\r\n";
outputStream.write(aaa.getBytes());
String bbb = String.format("Content-Type: %s\r\n\r\n", contentType);
outputStream.write(bbb.getBytes());
// 获取媒体文件的输入流(读取文件)
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte[] buf = new byte[8096];
int size = 0;
while ((size = bis.read(buf)) != -1) {
// 将媒体文件写到输出流(往微信服务器写数据)
outputStream.write(buf, 0, size);
}
// 请求体结束
outputStream.write(("\r\n--" + boundary + "--\r\n").getBytes());
outputStream.close();
bis.close();
// 获取媒体文件上传的输入流(从微信服务器读数据)
InputStream inputStream = uploadConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer buffer = new StringBuffer();
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放资源
inputStream.close();
uploadConn.disconnect();
System.out.println(buffer.toString());
return buffer.toString();
}