Reading directly from url - not working - java

In some code i use similar code to Oracle's tutorial: Reading directly from a URL. Oracle's code also here below:
import java.net.*;
import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://www.oracle.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
This code worked before but it doesnt anymore. Even if i try the exact same code, inputLine never has any value. I dont know if it has something to do with java versions or something elso but i would like to know why this happens, and what is a good alternative.

If you replace http://www.oracle.com/ by http://www.google.com/ it works.
It's because a GET http://www.oracle.com/ gives
HTTP/1.1 301 Moved Permanently
If you run
curl www.oracle.com
You obtain the same effect. You have to follow the redirect with
curl -L www.oracle.com
to obtain the html content. In Java, you also have to follow the redirect, such as in this article :
https://www.mkyong.com/java/java-httpurlconnection-follow-redirect-example/

Oracle -
When you run the program, you should see, scrolling by in your command
window, the HTML commands and textual content from the HTML file
located at http://www.oracle.com/. Alternatively, the program might
hang or you might see an exception stack trace. If either of the
latter two events occurs, you may have to set the proxy host so that
the program can find the Oracle server.
You can set the proxy host through the command line. Depending on
your network configuration, you might also need to set the proxy
port. If necessary, ask your system administrator for the name of the
proxy host on your network.
UNIX
java -Dhttp.proxyHost=proxyhost
[-Dhttp.proxyPort=portNumber] URLReader
DOS shell (Windows 95/NT)
java -Dhttp.proxyHost=proxyhost
[-Dhttp.proxyPort=portNumber] URLReader
Also, I have actually ran it on my IDE but it only works with HTTPS

As loris securo commented i needed to use https instead of http. And that fixed my problems.

Related

how to create a URL object using an InetAddress object in Java?

I have a need to create a http or https URL object from an IPv4 address (String or InetAddress objects - either one is ok) in Java. I have been at this for 10 hours now.
Attempts that hit a wall described below:
Attempt #1: I tried to make the URL by assembling a string, and then feeding it to a URL constructor.
Textbook states that a URL can be "protocol://host", with host being either a host name or IP address. but creating a URL like this: URL a = new URL("http://151.101.65.69"); and opening a stream to this URL (a) gives a HTTP error 500 (Internal Server Error - An unexpected condition occurred that the server does not know how to handle).
What get me fuming is that URL a = new URL("http://stackoverflow.com"); works.
At this point I am stuck. I have no Idea what to change, or how to move forward.
Attempt #2: I tried to do a reverse lookup on the IP address using "getHostName()" method in the InetAddress class.
this should return the host name by doing a reverse DNS lookup. Yet, I keep trying it for 151.101.65.69 (stackoverflow web server IP address), and the look up fails. By fails I mean the IP address is returned as string rather than the host name as a string. I read the Oracle docs http://docs.oracle.com/javase/1.5.0/docs/api/java/net/InetAddress.html#getHostName(), but I don't understand how to overcome the "security manager" the document mentions (or if it is indeed the reason the reverse lookup fails).
I also tried "getCannonicalHostName()", but that didn't fly either.
I am trying to figure out how to open a website using the IP address. It looks like my browser is running into the same issue as my code. I read up on How to access site through IP address when website is on a shared host? but I do not have any user names, as I want to be able to open any website that a user has an IP address for. Adding a port (such as 80) does not seem to work; neither does leaving the user name blank or using a generic 'user' or 'guest'.
I need is to create a URL object from an IPv4 String or InetAddress object, and I am stuck. I understand that a knowledgeable programmer such as you, may say that making URLs from IP addresses is not what IP addresses are for, or point out that I am not including a file portion of the URL, but that is not the problem at this moment. Could you please help me with my core challenge?
The answer provided by D.B. is good. I had very similar code; but you will find that this code will not work every time. There are IPv4 addresses you pass to the code offered D.B.'s answer which will not be able to open a URL stream (for example the IP address for stackoverflow). I thought the problem was my coding, and that is what I was hoping to get help with on stackoverflow. But I now realize the problem was my lack of understanding when asking this question. What I now understand, is that having an IPv4 address is not sufficient to open every website on the web. Anytime a server hosts multiple websites, the IP address can be used to connect to the server, but not to simultaneously identify the website we want to open/access. This gentleman explains this quite well: http://ask-leo.com/why_doesnt_accessing_a_site_by_its_ip_address_work.html
#D.B. thanks for taking the time to help. Much appreciated!
The following code works for me.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
public class InetAddressMain {
public static void main(String[] args) {
try {
InetAddress addr = InetAddress.getByName("172.217.4.110");
URL url = new URL("http://"+addr.getHostAddress());
InputStream is = url.openStream();
InputStreamReader isReader = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isReader);
String line;
while((line = reader.readLine()) != null){
System.out.println(line);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta content="Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for." ... [output shortened for readability]

How to intercept SSH Password request in a Java Program

I am need to spawn a SSH connection from a JAVA program using ProcessBuilder and a USERID/PASSWORD combination.
I have already successfully implemented SSH connections using Ganymed, JSch, a combination of JAVA Processbuilder and Expect scripting (Expect4J also), JAVA ProcessBuilder and SSHPASS script and SSH Shared Key.
Security is NOT a concern at this point in time and all I am after is to be able to support programmatically all kinds of combinations for SSH connection.
My problem is the Password prompt that SSH throws somewhere that is not on STDIN/STDOUT (on a tty I believe). This is my last hurdle to overcome.
My question is there a way to intercept SSH password request and provide it from my JAVA code?
Please, note this is a very narrow question (and all the above information was to guarantee the answer would not be too broad).
Here is a sample code of what I am trying:
import java.io.*;
import java.util.*;
public class ProcessBuilderTest {
public static void main(String[] args) throws IOException, Exception {
ProcessBuilder pb = new ProcessBuilder(
"/usr/bin/ssh",
"nyuser#myserver.com",
"export NOME='Jennifer Lawrence'; echo $NOME"
);
pb.redirectErrorStream(); //redirect stderr to stdout
Process process = pb.start();
InputStream inputStream = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
while((line = reader.readLine())!= null) {
System.out.println(line);
}
process.waitFor();
}
}
But, when I run it I got this:
[memphis BuilderTest]# java ProcessBuilderTest
myuser#myserver's password:
and after I type the password, I got the rest of the output:
Jennifer Lawrence
[memphis BuilderTest]#
Again, the specific question is:
Is there a way to spawn an external ssh client (OpenSSH, Tectia SSH, SecureCRT, etc) using PasswordAuthentication method (no other method can be used) process using JAVA ProcessBuilder interface (no other language can be used), intercept/capture the password prompt and respond/interact providing that password from my JAVA code (so the user does not need to type it)?
You need to learn about pseudo-ttys, assuming that you are operating on Linux. The password prompt is on the tty device. You will need to build a separate process running against a pseudo-tty instead of just inheriting your tty device, and then you can intercept the password prompt.
This is a moderately complex process.
There is a library that supports some of this: http://www.ganymed.ethz.ch/ssh2/FAQ.html. You might find reading its source illuminating if it is available.
While it has been suggested that a pseudo-tty (pty) is required to simulate a terminal, the accepted answer doesn't provide a working solution - there are also lots of similar questions with no working answers.
Here are two solutions that allow you to capture the "Password:" prompt in SSH and enter the password in an automated way without using SSH_ASKPASS or Expect.
Why use one programming language when you can use two - the first option isn't ideal, but it demonstrates the solution:
ProcessBuilder pb = new ProcessBuilder("/usr/bin/python", "-c", "import pty; pty.spawn(['/usr/bin/ssh', '<hostname>'])");
The above example makes use of the Python pty module to wrap SSH into a PTY. Although it is simple, it doesn't provide any flexibility to allow you to modify any terminal properties like the passed window size.
The other more lightweight option is to use a PTY wrapper in C - the pty tool from the "Advanced Programming in the UNIX® Environment" book is just this - the source can be found at https://github.com/abligh/pty.
You will then use it in a similar way, but referencing the pty tool instead of Python:
ProcessBuilder pb = new ProcessBuilder("/usr/local/bin/pty", "/usr/bin/ssh", "<hostname>");
This is the same approach that Expect uses to simulate a PTY, which is why you are able to intercept it using Expect. It goes without saying that tunneled clear text passwords are insecure and public key authentication should always be the preferred way of doing this.

Communication between two Machines using java

I have Gui Application written which running on windows,and i want to connect to remote unix machine and perform actions there such like API's ,go over the log file in the machines and send back to the application the last log file or others API that i want to perform on the remote machine.
In the remote machine i don;t have application server i just have Java which installed there.
I want to use Java in order to perform remote API over the remote machine;
what is the advice ,can i use web services ,can any one please advise.
Thanks in advance.
If Java can perform the actions you're talking about, I would use Sockets to communicate with the UNIX-Machine (over TCP/IP).
Your Windows-PC would be the client sending commands to the Unix-PC.
Web services would be a bit heavy handed option, esp if you opt for the SOAP ones. If you don't have a problem with the client and server always being Java, RMI seems to be the simplest solution to this problem since it's communication between two different JVM's using the normal method calling mechanism (with some additional interfaces and rules to be followed to please the RMI specification).
The Spring Framework ships with a number of remoting options that are all very easy to setup. You can use their classes for simpler configuration of something standard like RMI or JMS, or use a lightweight web services protocol such as Spring's HTTP invoker or Hessian.
For analyzing log files of remote machines you can always use Apache Commons sftp programmatically to FTP a copy of the remote log file to your PC.
If you configure the log files to be rotatable or to rotate each time they reach a specific size, you can avoid reloading the same information over and over.
You can use Ganymed SSH-2 for Java to ssh to the remote host from Client Java App and run the commands. No need to run any additional components on remote server. You can do password based authentication or key based authentication to login to remote host. We had successfully used it to administer (start/stop/grep log files, etc.) applications running on remote UNIX hosts. You can capture output of the remote command using the StreamGobbler class provided in the package. You can pass multiple commands separated by semi-colon in one remote call.
Basic Example included in the package:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class Basic
{
public static void main(String[] args)
{
String hostname = "127.0.0.1";
String username = "joe";
String password = "joespass";
try
{
/* Create a connection instance */
Connection conn = new Connection(hostname);
/* Now connect */
conn.connect();
/* Authenticate.
* If you get an IOException saying something like
* "Authentication method password not supported by the server at this stage."
* then please check the FAQ.
*/
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
/* Create a session */
Session sess = conn.openSession();
sess.execCommand("uname -a && date && uptime && who");
System.out.println("Here is some information about the remote host:");
/*
* This basic example does not handle stderr, which is sometimes dangerous
* (please read the FAQ).
*/
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while (true)
{
String line = br.readLine();
if (line == null)
break;
System.out.println(line);
}
/* Show exit status, if available (otherwise "null") */
System.out.println("ExitCode: " + sess.getExitStatus());
/* Close this session */
sess.close();
/* Close the connection */
conn.close();
}
catch (IOException e)
{
e.printStackTrace(System.err);
System.exit(2);
}
}
}

Suddenly can't connect to external server in Java

It's been working perfectly fine all day long, now suddenly I can't connect. I can connect through the browser (and so can you: http://secure.exoterragame.com/noxastra/login.php), but it won't work in my Java application. I get an UnknownHostException.
URL register = new URL("http://secure.exoterragame.com/noxastra/login.php" +
"?username=" + request.Username +
"&password=" + request.Password);
URLConnection conn = register.openConnection();
I am completely stumped. Does anyone know why this would happen?
(Yes, we'll be using https in the final version :P)
Do a packet sniff to see what's going on behind the scenes. (I recommend Wireshark for all platforms.)
I had a similar problem in Flash once and was tearing my hair until I realized it was a bug in the VM and it was giving me timeouts without ever sending any packets!
I don't think Java will have such a bug, but the point is, sniffing is sometimes invaluable.
Maybe there's a DNS/hosts-file update which your browser has gotten, but the program not (or the other way around?) Maybe the program's getting stuck on a (local) firewall? Maybe it does login successfully but breaks on a redirect response? (I don't even know if URL is capable of doing that, but still...) Maybe you've enabled/disabled IPv6 somewhere?
jcomeau#intrepid:/tmp$ cat test.java; javac test.java; java test
import java.net.*;
public class test {
public static void main(String args[]) throws Exception {
URL register = new URL(
"http://secure.exoterragame.com/noxastra/login.php" +
"?username=" + "guest" +
"&password=" + "guest");
URLConnection conn = register.openConnection();
}
}
Doesn't throw an exception for me. Perhaps https://stackoverflow.com/users/800237/adithya-surampudi 's suggestion will work.

How can I make a java applet connect with a server?

Please excuse my noobishness as I am teaching myself Java and don't know a lot.
I'm trying to make a multiplayer game that runs from Java applets, I have a server-side program working that will accept strings of text, but all my attempts to find code for applets have failed.
My best attempt looks like it works but I think fails to connect to the server, any ideas why? (localIP is my correct IP and works fine in other tests)
public void init()
{
try
{
socket = new Socket(localIP, 5555);
inStream = new DataInputStream(socket.getInputStream());
outStream = new PrintStream(socket.getOutputStream());
}
catch(Exception e)
{
never reached
}
}
I don't mind scrapping this if someone can tell me a better way to do it or any way at all.
a java applet can only connect to the server from which it was downloaded. if you are not loading the applet from localIP, then you will not be able to connect to it.
you may be able to get around this restriction by signing the applet.
Given that you are not using the Http Protocol, One assumes that the applet is loaded from another port other than 5555. If this is the case, the applet needs to be signed in order to do this functionality.

Categories

Resources