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

  

原文地址:https://www.cnblogs.com/jayGold/p/3487876.html