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