Name: pa48320 Date: 08/05/2002
I am trying to write some code that accesses an https site by redirecting through a login site. When the httpsConnection follows the redirect I get an error that there is no protocol:
java.net.MalformedURLException: no protocol: /servlet/SessionServlet?ssaction=showLoginInvalid&url=/partner/bugs/data/bugs/4049125.html
at java.net.URL.<init>(URL.java:579)
at java.net.URL.<init>(URL.java:476)
at java.net.URL.<init>(URL.java:425)
at sun.net.www.protocol.http.HttpURLConnection.followRedirect(HttpURLConnection.java:1074)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:668)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(DashoA6275)
at HTMLScrapeMain$HTMLScrapeMainInnerAdapter0.JButton1ActionPerformedHandler1(HTMLScrapeMain.java:369)
If I turn off InstanceFollowsRedirect then I get a response back of "Temporarily Moved". This is in 1.4.1. Any ideas what needs to be set for this to work?
Here's the code:
java.net.URL u = new java.net.URL("https", "javapartner.sun.com", -1, "/servlet/SessionServlet");
java.net.HttpURLConnection uc = (java.net.HttpURLConnection)u.openConnection();
uc.setRequestMethod("POST");
// uc.connect();
// Make sure browser doesn't cache this URL.
uc.setUseCaches(false);
uc.setFollowRedirects(false);
uc.setInstanceFollowRedirects(false);
// Tell browser to allow me to send data to server.
uc.setDoOutput(true);
ByteArrayOutputStream byteStream =
new ByteArrayOutputStream(512); // Grows if necessary
// Stream that writes into buffer
PrintWriter out = new PrintWriter(byteStream, true);
String postData =
"ssaction="+URLEncoder.encode("login")+
"&url=" + URLEncoder.encode("/partner/bugs/data/bugs/4049125.html") +
"&UserId=" + URLEncoder.encode("<userID>")+
"&Password="+URLEncoder.encode("<passwd>");
// Write POST data into local buffer
out.print(postData);
out.flush(); // Flush since above used print, not println
// POST requests are required to have Content-Length
String lengthString =
String.valueOf(byteStream.size());
uc.setRequestProperty("Content-Length", lengthString);
uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// Write POST data to real output stream
byteStream.writeTo(uc.getOutputStream());
BufferedReader in =
new BufferedReader(new InputStreamReader
(uc.getInputStream()));
String line;
String linefeed = "\n";
outField.setText("");
while((line = in.readLine()) != null)
{
outField.append(line);
outField.append(linefeed);
}
System.out.println(uc.getResponseMessage());
uc.disconnect();
======================================================================