zoukankan      html  css  js  c++  java
  • OAuth2.0 https

    	/**
    	 * OAuth 认证
    	 * @throws Exception
    	 */
    	public String requestYimallOAuth() throws Exception {
    		
    		TrustManager[] trustAllCerts = new TrustManager[] {
    				   new X509TrustManager() {
    				      public java.security.cert.X509Certificate[] getAcceptedIssuers() {
    				        return null;
    				      }
    
    				      public void checkClientTrusted(X509Certificate[] certs, String authType) {  }
    
    				      public void checkServerTrusted(X509Certificate[] certs, String authType) {  }
    
    				   }
    				};
    		
    			SSLContext sc;
    			sc = SSLContext.getInstance("SSL");
    			
    			sc.init(null, trustAllCerts, new java.security.SecureRandom());
    			HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    
    			// Create all-trusting host name verifier
    			HostnameVerifier allHostsValid = new HostnameVerifier() {
    			    public boolean verify(String hostname, SSLSession session) {
    			      return true;
    			    }
    			};
    			// Install the all-trusting host verifier
    			HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    			
    			URL url = new URL("");//  你要请求的地址
    			HttpsURLConnection httpsURLConnection=(HttpsURLConnection)url.openConnection();
    			httpsURLConnection.setConnectTimeout(30000);
    			httpsURLConnection.setReadTimeout(30000);
    			httpsURLConnection.setDoOutput(true);
    			httpsURLConnection.setDoInput(true); 
    			httpsURLConnection.setUseCaches(true);   
    			httpsURLConnection.setRequestMethod("POST");
    
    			httpsURLConnection.connect();
    
    			int responseCode=httpsURLConnection.getResponseCode();
    			InputStream input=null;
    			if(responseCode==200){
    				input=httpsURLConnection.getInputStream();
    			}else{
    				input=httpsURLConnection.getErrorStream();
    			}
    			BufferedReader in = new BufferedReader(new InputStreamReader(input));
    			StringBuilder result=new StringBuilder();
    			String line=null; 
    			while((line=in.readLine())!=null){
    				result.append(line);
    			}
    			
    			String access_token = result.toString();
    			System.out.println(access_token);
    			return "success";
    	}
    

      

  • 相关阅读:
    SVN使用教程总结
    SVN
    js中设置元素class的三种方法小结
    Javascript 删除tr 元素
    SQL Server执行计划的理解
    java多线程知识点汇总(四)多线程知识点脉络图
    hibernate将connection放进threadlocal里实现数据库连接池
    数据库连接池中是将connection放进threadlocal里的
    java jdbc深入理解(connection与threadlocal与数据库连接池和事务实)
    java项目怎样添加jar包依赖?
  • 原文地址:https://www.cnblogs.com/jayGold/p/3487876.html
Copyright © 2011-2022 走看看