Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help!! unable to getInputStream from HTTPS page.

Status
Not open for further replies.

ZPlay

Technical User
Jan 10, 2002
24
0
0
US
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 = &quot;&quot;; 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
 
401 is thrown for unauthorised access. I would assume that there is some specific way of gaining an SSL connection as casting url.getConnection() probably won't.

Not sure...try gaining an SSLSocket first then interacting with it. Jeremy Nicholson, Director of a UK-based Java and Data Warehousing consultancy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top