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";
    	}
    

      

  • 相关阅读:
    深入理解JVM内幕:从基本结构到Java 7新特性
    通过Java反射做实体查询
    Hadoop教程(一)
    很不错的js特效
    java utf8字符 导出csv 文件的乱码问题。
    spring MVC使用Interceptor做用户登录判断
    Bootstrap--全局css样式之图片
    Bootstrap-全局css样式之按钮
    Bootstrap--全局css样式之表单
    Bootstarp--全局CSS样式之表格
  • 原文地址:https://www.cnblogs.com/jayGold/p/3487876.html
Copyright © 2011-2022 走看看