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

      

  • 相关阅读:
    微信推送给服务器的XML消息解析-springmvc 解析xml数据流
    request.getInputStream() 的两种解析方式
    微信的token验证
    springmvc 解析xml数据
    Spring 定时器 No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined
    纯CSS实现图片
    Java线程池应用
    JavaScript 插件的书页翻转效果
    c语言中字符串函数的使用
    窗体显示类
  • 原文地址:https://www.cnblogs.com/jayGold/p/3487876.html
Copyright © 2020-2023  润新知