Home | History | Annotate | Download | only in http
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.squareup.okhttp.internal.http;
     18 
     19 import com.squareup.okhttp.OkHttpClient;
     20 import com.squareup.okhttp.Protocol;
     21 import java.io.BufferedReader;
     22 import java.io.InputStreamReader;
     23 import java.net.URL;
     24 import java.util.List;
     25 import javax.net.ssl.HostnameVerifier;
     26 import javax.net.ssl.HttpsURLConnection;
     27 import javax.net.ssl.SSLSession;
     28 
     29 import static com.squareup.okhttp.internal.http.OkHeaders.SELECTED_PROTOCOL;
     30 
     31 public final class ExternalHttp2Example {
     32   public static void main(String[] args) throws Exception {
     33     URL url = new URL("https://http2.iijplus.jp/push/test1");
     34     HttpsURLConnection connection = (HttpsURLConnection) new OkHttpClient()
     35         .setProtocols(Protocol.HTTP2_AND_HTTP_11).open(url);
     36 
     37     connection.setHostnameVerifier(new HostnameVerifier() {
     38       @Override public boolean verify(String s, SSLSession sslSession) {
     39         System.out.println("VERIFYING " + s);
     40         return true;
     41       }
     42     });
     43 
     44     int responseCode = connection.getResponseCode();
     45     System.out.println(responseCode);
     46     List<String> protocolValues = connection.getHeaderFields().get(SELECTED_PROTOCOL);
     47     // If null, probably you didn't add jetty's npn jar to your boot classpath!
     48     if (protocolValues != null && !protocolValues.isEmpty()) {
     49       System.out.println("PROTOCOL " + protocolValues.get(0));
     50     }
     51 
     52     BufferedReader reader =
     53         new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
     54     String line;
     55     while ((line = reader.readLine()) != null) {
     56       System.out.println(line);
     57     }
     58   }
     59 }
     60