URLConnectionHeaders.java revision 2362
0N/A/*
1472N/A * Copyright (c) 2001, 2002, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
1472N/A * published by the Free Software Foundation.
0N/A *
1472N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
1472N/A */
1472N/A
0N/A/*
0N/A * @test
0N/A * @bug 4143541 4147035 4244362
0N/A * @summary URLConnection cannot enumerate request properties,
0N/A * and URLConnection can neither get nor set multiple
0N/A * request properties w/ same key
0N/A *
0N/A */
0N/A
0N/Aimport java.net.*;
0N/Aimport java.util.*;
0N/Aimport java.io.*;
0N/A
0N/Apublic class URLConnectionHeaders {
static class XServer extends Thread {
ServerSocket srv;
Socket s;
InputStream is;
OutputStream os;
XServer (ServerSocket s) {
srv = s;
}
Socket getSocket () {
return (s);
}
public void run() {
try {
String x;
s = srv.accept ();
is = s.getInputStream ();
BufferedReader r = new BufferedReader(new InputStreamReader(is));
os = s.getOutputStream ();
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(os));
w.write("HTTP/1.1 200 OK\r\n");
w.write("Content-Type: text/plain\r\n");
while ((x=r.readLine()).length() != 0) {
System.out.println("request: "+x);
if (!x.startsWith("GET")) {
w.write(x);
w.newLine();
}
}
w.newLine();
w.flush();
s.close ();
srv.close (); // or else the HTTPURLConnection will retry
} catch (IOException e) { e.printStackTrace();}
}
}
public static void main(String[] args) {
try {
ServerSocket serversocket = new ServerSocket (0);
int port = serversocket.getLocalPort ();
XServer server = new XServer (serversocket);
server.start ();
Thread.sleep (200);
URL url = new URL ("http://localhost:"+port+"/index.html");
URLConnection uc = url.openConnection ();
// add request properties
uc.addRequestProperty("Cookie", "cookie1");
uc.addRequestProperty("Cookie", "cookie2");
uc.addRequestProperty("Cookie", "cookie3");
Map e = uc.getRequestProperties();
if (!((List)e.get("Cookie")).toString().equals("[cookie3, cookie2, cookie1]")) {
throw new RuntimeException("getRequestProperties fails");
}
e = uc.getHeaderFields();
if ((e.get("Content-Type") == null) || (e.get(null) == null)) {
throw new RuntimeException("getHeaderFields fails");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}