1/* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26import java.io.IOException; 27import java.net.Authenticator; 28import java.net.HttpURLConnection; 29import java.net.InetSocketAddress; 30import java.net.Proxy; 31import java.net.URL; 32import javax.net.ssl.HttpsURLConnection; 33 34/** 35 * A simple Http client that connects to the HTTPTestServer. 36 * @author danielfuchs 37 */ 38public class HTTPTestClient extends HTTPTest { 39 40 public static void connect(HttpProtocolType protocol, 41 HTTPTestServer server, 42 HttpAuthType authType, 43 Authenticator auth) 44 throws IOException { 45 46 InetSocketAddress address = server.getAddress(); 47 final URL url = url(protocol, address, "/"); 48 final Proxy proxy = proxy(server, authType); 49 50 System.out.println("Client: FIRST request: " + url + " GET"); 51 HttpURLConnection conn = openConnection(url, authType, proxy); 52 configure(conn, auth); 53 System.out.println("Response code: " + conn.getResponseCode()); 54 String result = new String(conn.getInputStream().readAllBytes(), "UTF-8"); 55 System.out.println("Response body: " + result); 56 if (!result.isEmpty()) { 57 throw new RuntimeException("Unexpected response to GET: " + result); 58 } 59 System.out.println("\nClient: NEXT request: " + url + " POST"); 60 conn = openConnection(url, authType, proxy); 61 configure(conn, auth); 62 conn.setRequestMethod("POST"); 63 conn.setDoOutput(true); 64 conn.setDoInput(true); 65 conn.getOutputStream().write("Hello World!".getBytes("UTF-8")); 66 System.out.println("Response code: " + conn.getResponseCode()); 67 result = new String(conn.getInputStream().readAllBytes(), "UTF-8"); 68 System.out.println("Response body: " + result); 69 if ("Hello World!".equals(result)) { 70 System.out.println("Test passed!"); 71 } else { 72 throw new RuntimeException("Unexpected response to POST: " + result); 73 } 74 } 75 76 private static void configure(HttpURLConnection conn, Authenticator auth) 77 throws IOException { 78 if (auth != null) { 79 conn.setAuthenticator(auth); 80 } 81 if (conn instanceof HttpsURLConnection) { 82 System.out.println("Client: configuring SSL connection"); 83 // We have set a default SSLContext so we don't need to do 84 // anything here. Otherwise it could look like: 85 // HttpsURLConnection httpsConn = (HttpsURLConnection)conn; 86 // httpsConn.setSSLSocketFactory( 87 // new SimpleSSLContext().get().getSocketFactory()); 88 } 89 } 90 91} 92