test/sun/net/www/protocol/http/B6369510.java

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. --- 1,7 ---- /* ! * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.
*** 38,48 **** public class B6369510 { com.sun.net.httpserver.HttpServer httpServer; ExecutorService executorService; ! public static void main(String[] args) { new B6369510(); } public B6369510() --- 38,48 ---- public class B6369510 { com.sun.net.httpserver.HttpServer httpServer; ExecutorService executorService; ! public static void main(String[] args) throws Exception { new B6369510(); } public B6369510()
*** 56,72 **** } void doClient() { try { InetSocketAddress address = httpServer.getAddress(); // GET Request ! URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test/"); HttpURLConnection uc = (HttpURLConnection)url.openConnection(); int resp = uc.getResponseCode(); if (resp != 200) ! throw new RuntimeException("Failed: Response code from GET is not 200"); System.out.println("Response code from GET = 200 OK"); //POST Request uc = (HttpURLConnection)url.openConnection(); --- 56,74 ---- } void doClient() { try { InetSocketAddress address = httpServer.getAddress(); + String urlString = "http://" + InetAddress.getLocalHost().getHostName() + ":" + address.getPort() + "/test/"; + System.out.println("URL == " + urlString); // GET Request ! URL url = new URL("http://" + InetAddress.getLocalHost().getHostName() + ":" + address.getPort() + "/test/"); HttpURLConnection uc = (HttpURLConnection)url.openConnection(); int resp = uc.getResponseCode(); if (resp != 200) ! throw new RuntimeException("Failed: Response code from GET is not 200 RSP == " + resp); System.out.println("Response code from GET = 200 OK"); //POST Request uc = (HttpURLConnection)url.openConnection();
*** 73,88 **** uc.setDoOutput(true); uc.setRequestMethod("POST"); OutputStream os = uc.getOutputStream(); resp = uc.getResponseCode(); if (resp != 200) ! throw new RuntimeException("Failed: Response code form POST is not 200"); System.out.println("Response code from POST = 200 OK"); } catch (IOException e) { e.printStackTrace(); } finally { httpServer.stop(1); executorService.shutdown(); } } --- 75,91 ---- uc.setDoOutput(true); uc.setRequestMethod("POST"); OutputStream os = uc.getOutputStream(); resp = uc.getResponseCode(); if (resp != 200) ! throw new RuntimeException("Failed: Response code form POST is not 200 RSP == " + resp); System.out.println("Response code from POST = 200 OK"); } catch (IOException e) { e.printStackTrace(); + throw new RuntimeException("Failed with IOException"); } finally { httpServer.stop(1); executorService.shutdown(); } }