This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Read from a URL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Read from a URL" Watch "Read from a URL" New topic

Read from a URL

Trista Lau

Joined: Jan 18, 2010
Posts: 2
hello, I'm trying to get the text of this URL in Java:
Then I wrote the code like this:
URL url = new URL("");
}catch ...

It failed as URL class cannot recognize the filename The following variables of the URL are as follows:
protocol = https
authority =
host =
port = -1
path = b5/forms_lcs71_online.php
query = act_code=40216827
filename = b5/forms_lcs71_online.php?act_code=40216827
ref = null
Could anyone suggest me a way to read a URL that is not ended with file extensions (.html, .txt....)?
Thank you.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

I clicked the URL and Firefox pointed out the self-signed certificate -- i.e., the https installation is suspect. I don't know if that's what's giving you problems, or if you just haven't taken the necessary steps to handle https in general. See here for details about that.

In any case, it has nothing to do with "recognizing the filename" -- the format of the part of the URL after the server name is irrelevant. It's up to the server to understand it, not your code.

[Jess in Action][AskingGoodQuestions]
Trista Lau

Joined: Jan 18, 2010
Posts: 2
Thanks for your instant and helpful reply.

Yes, the URL I need to get is a web form provided by the government for citizens to apply courses.
The form needs authentication for security. And, an exception is caught.

In the article you give to me, Java Tip 96: Use HTTPS in your Java client code(Page 2),
"What about applets?
Performing HTTPS-based communication from within an applet seems like a natural extension of scenarios described above. In reality, it's even easier in most cases. In 4.0 and later versions of Netscape Navigator and Internet Explorer, HTTPS is enabled by default for their respective VMs. ... simply specify HTTPS as your protocol when creating an instance of the URL class:
URL url = new URL("https://[your server]");"

Does it mean if I write an Applet, instead of Java Application, I need not worry about https?

I think https protocol can be understood by Java URL class.
But, what I worry is the red part cannot be understood:
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

As I said, the red part is completely ignored by your Java code.

Why not tell us the actual error message you're receiving?
I agree. Here's the link:
subject: Read from a URL
jQuery in Action, 3rd edition