I'm unable to getInputStream() from a password protected HTTPS page. The id and password are included in the url string. I use the same url string to access the page just fine in the browser but when I use code to automate the process I keep getting this error:
java.io.IOException: Server returned HTTP response code: 401 for URL:
My code works fine on other HTTPS pages that are not password protected. Just to reiterate, the id and pw needed to access the page are in the url string. This allows me to bypass the login prompt. It works fine in a browser but gives me an error in my code. My code is as follows:
import java.net.*;
import java.io.*;
import java.util.*;
import javax.net.ssl.*;
public class testit
{
public static void main(String[] args)
throws MalformedURLException, IOException
{
URL url = new URL(args[0]);
HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
printinfo(conn);
}
public static void printinfo(HttpsURLConnection lConn)
throws IOException
{
System.out.println("URL: " +
lConn.getURL().toExternalForm() + ":"
System.out.println("Content Type: " +
lConn.getContentType());
System.out.println("Content Length: " +
lConn.getContentLength());
System.out.println("Last Modified: " +
new Date(lConn.getLastModified()));
System.out.println("Expiration: " +
lConn.getExpiration());
System.out.println("Content Encoding: " +
lConn.getContentEncoding());
System.out.println("Cipher: " +
lConn.getCipherSuite());
System.out.println("\n\nContent:\n"
DataInputStream in = new DataInputStream(lConn.getInputStream()); // <--ERROR OCCURS HERE
String line = ""; int k =0;
while((line = in.readLine()) != null && k < 20)
{
System.out.println(line);
k ++;
}
}
}
The error occurs at the getInputStream().
Help please! Thanks, Dan
java.io.IOException: Server returned HTTP response code: 401 for URL:
My code works fine on other HTTPS pages that are not password protected. Just to reiterate, the id and pw needed to access the page are in the url string. This allows me to bypass the login prompt. It works fine in a browser but gives me an error in my code. My code is as follows:
import java.net.*;
import java.io.*;
import java.util.*;
import javax.net.ssl.*;
public class testit
{
public static void main(String[] args)
throws MalformedURLException, IOException
{
URL url = new URL(args[0]);
HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
printinfo(conn);
}
public static void printinfo(HttpsURLConnection lConn)
throws IOException
{
System.out.println("URL: " +
lConn.getURL().toExternalForm() + ":"
System.out.println("Content Type: " +
lConn.getContentType());
System.out.println("Content Length: " +
lConn.getContentLength());
System.out.println("Last Modified: " +
new Date(lConn.getLastModified()));
System.out.println("Expiration: " +
lConn.getExpiration());
System.out.println("Content Encoding: " +
lConn.getContentEncoding());
System.out.println("Cipher: " +
lConn.getCipherSuite());
System.out.println("\n\nContent:\n"
DataInputStream in = new DataInputStream(lConn.getInputStream()); // <--ERROR OCCURS HERE
String line = ""; int k =0;
while((line = in.readLine()) != null && k < 20)
{
System.out.println(line);
k ++;
}
}
}
The error occurs at the getInputStream().
Help please! Thanks, Dan