1c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath/*
2c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * Copyright (C) 2009 The Android Open Source Project
3c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath *
4c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * Licensed under the Apache License, Version 2.0 (the "License");
5c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * you may not use this file except in compliance with the License.
6c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * You may obtain a copy of the License at
7c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath *
8c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath *      http://www.apache.org/licenses/LICENSE-2.0
9c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath *
10c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * Unless required by applicable law or agreed to in writing, software
11c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * distributed under the License is distributed on an "AS IS" BASIS,
12c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * See the License for the specific language governing permissions and
14c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath * limitations under the License.
15c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath */
16c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath
172231db3e6bb54447a9b14cf004a6cb03c373651cjwilsonpackage com.squareup.okhttp.internal.http;
18c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath
192231db3e6bb54447a9b14cf004a6cb03c373651cjwilsonimport com.squareup.okhttp.OkHttpClient;
203c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport com.squareup.okhttp.Protocol;
21c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamathimport java.io.BufferedReader;
22c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamathimport java.io.InputStreamReader;
23c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamathimport java.net.URL;
243c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.util.List;
25c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamathimport javax.net.ssl.HostnameVerifier;
262231db3e6bb54447a9b14cf004a6cb03c373651cjwilsonimport javax.net.ssl.HttpsURLConnection;
27c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamathimport javax.net.ssl.SSLSession;
28c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath
293c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport static com.squareup.okhttp.internal.http.OkHeaders.SELECTED_PROTOCOL;
303c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
31c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamathpublic final class ExternalSpdyExample {
3254cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  public static void main(String[] args) throws Exception {
3354cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    URL url = new URL("https://www.google.ca/");
343c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller    HttpsURLConnection connection = (HttpsURLConnection) new OkHttpClient()
353c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller        .setProtocols(Protocol.SPDY3_AND_HTTP11).open(url);
36c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath
3754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    connection.setHostnameVerifier(new HostnameVerifier() {
3854cf3446000fdcf88a9e62724f7deb0282e98da1jwilson      @Override public boolean verify(String s, SSLSession sslSession) {
3954cf3446000fdcf88a9e62724f7deb0282e98da1jwilson        System.out.println("VERIFYING " + s);
4054cf3446000fdcf88a9e62724f7deb0282e98da1jwilson        return true;
4154cf3446000fdcf88a9e62724f7deb0282e98da1jwilson      }
4254cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    });
43c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath
4454cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    int responseCode = connection.getResponseCode();
4554cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    System.out.println(responseCode);
463c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller    List<String> protocolValues = connection.getHeaderFields().get(SELECTED_PROTOCOL);
473c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller    // If null, probably you didn't add jetty's npn jar to your boot classpath!
483c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller    if (protocolValues != null && !protocolValues.isEmpty()) {
493c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller      System.out.println("PROTOCOL " + protocolValues.get(0));
503c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller    }
51c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath
5254cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    BufferedReader reader =
5354cf3446000fdcf88a9e62724f7deb0282e98da1jwilson        new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
5454cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    String line;
5554cf3446000fdcf88a9e62724f7deb0282e98da1jwilson    while ((line = reader.readLine()) != null) {
5654cf3446000fdcf88a9e62724f7deb0282e98da1jwilson      System.out.println(line);
57c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath    }
5854cf3446000fdcf88a9e62724f7deb0282e98da1jwilson  }
59c3f6f16bd4a2338e88275641b9f2f56e816ca377Narayan Kamath}
60