Get content from external web page on java - java

I have a following EJB:
#Stateless
public class SomeService {
public String someOperation() {
...
}
}
So, the problem is to get content of external web page (for example http://example.com/some/link/info.xml) and return it. I need something like this:
String pageContent = SomeFancyLibrary.getPageContent(someurl);
Sorry if this question has been already asked here, but I didnt find any info.

From: https://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html
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();
}
}

Related

URLConnectionReader produces UnknownHostException

Thanks in advance for every input!
I'm getting a little familiar with how to read data from websites with Java and have tried to do this by reading data using a URLConnectionReader.
Unfortunately I get an UnknownHostException when I test the whole thing in a Java online compiler (https://www.jdoodle.com/online-java-compiler/).
Have I forgotten any imports? I proceeded according to a tutorial.
Code: (designed for online-java-compiler jdoodle):
import java.net.*;
import java.io.*;
public class URLConnectionReader {
public static void main(String[] args)
{
String output = getUrlContents("https://www.tradegate.de/orderbuch_umsaetze.php?isin=NO0010892359");
System.out.println(output);
}
private static String getUrlContents(String theUrl)
{
StringBuilder content = new StringBuilder();
try
{
URL url = new URL(theUrl);
URLConnection urlConnection = url.openConnection();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null)
{
content.append(line + "\n");
}
bufferedReader.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return content.toString();
}
}
Error message:
java.net.UnknownHostException: www.tradegate.de
at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:220)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
at java.base/java.net.Socket.connect(Socket.java:591)
at java.base/sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:285)
at java.base/sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:173)
at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:182)
at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:474)
at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:569)
at java.base/sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:265)
at java.base/sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:372)
at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1187)
at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1081)
at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1587)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1515)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:250)
at URLConnectionReader.getUrlContents(URLConnectionReader.java:21)
at URLConnectionReader.main(URLConnectionReader.java:8)
I separated the classes as follows and your code works without any exceptions=>
class Mian:
public class Mian {
public static void main(String[] args) throws ClassNotFoundException {
URLConnectionReader urlcr = new URLConnectionReader();
String output =
urlcr.getUrlContents("https://www.tradegate.de/orderbuch_umsaetze.php?
isin=NO0010892359");
System.out.println(output);
}
}
and URLConnectionReader class:
import java.net.*;
import java.io.*;
public class URLConnectionReader {
public String getUrlContents(String theUrl)
{
StringBuilder content = new StringBuilder();
try
{
URL url = new URL(theUrl);
URLConnection urlConnection = url.openConnection();
BufferedReader bufferedReader = new BufferedReader(new
InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null)
{
content.append(line + "\n");
}
bufferedReader.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return content.toString();
}
}

How to send data java and html

code JAVA
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class test{
public static void main (String[] args)throws Exception{
demoReadall();
}
public static void demoReadall(){
String dates;
String res;
String status;
try{
FileInputStream fstream = new FileInputStream("RunningSystem.log");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = br.readLine()) != null) {
String[] LineArray = strLine.split("\\|");
if("BATCH_PAYMENT".equals(LineArray[1])){
dates = LineArray[2];
res = LineArray[3];
status = LineArray[4];
System.out.println(dates);
System.out.println (res);
System.out.println (status);
}
}
fstream.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
How do you write the front-end code to retrieve information about this code? Please bother me.
I would like to know how to send java and html data, please advise writing and sending data.
Read about .jsp, servlets or Spring framework ;-)

How to copy the contents of a html div from a website using Java

I'm trying to write a function in java that will basically copy and paste the html code from a div from a url. The data in question is from http://cdn.espn.com/sports/scores#completed however the data is not viewable when copied into my function using io streams. The data itsself is is viewable when I click inspect and control-f "completed-soccer" it shows up as but my code does not retrieve it at all. Here is the code i used.
package project;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class DownloadPage {
public static void main(String[] args) throws IOException {
// Make a URL to the web page
URL url = new URL("http://cdn.espn.com/sports/scores#completed-soccer");
// Get the input stream through URL Connection
URLConnection con = url.openConnection();
InputStream is =con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
// read each line and write to System.out
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
If you can't reach the data with normal HTTP request, you have to use more complex library, as Selenium with a Webdriver.
This library allows you to really navigate in a webpage, execute javascript and inspect all the element.
You can find a lot of information and guides.
Try with this code
public static void main(String[] args) throws IOException {
URL url = new URL("http://cdn.espn.com/sports/scores#completed-soccer");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try
{
InputStream in = url.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder result = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
}
finally
{
urlConnection.disconnect();
}
}

How to read specific line value from a website?

Following code read whole data set from a website.But I want to read specific value record from a web site. As a example when I pass a word to a website it dynamically display weather the word which I entered was what kind of word.When I pass tree name this return biographical name.I want to read that biographical name.Not the all data set of a website.How should I change?
import java.net.*;
import java.io.*;
public class DataRecord {
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();
}
}

How can I get HTML content from a specific URL on server side by using Java?

I am designing an application that needs to load HTML content from a specific URL on server side by using Java. How can I solve it?
Regards,
I have used the Apache Commons HttpClient library to do this. Have a look here:
http://hc.apache.org/httpclient-3.x/tutorial.html
It is more feature rich than the JDK HTTP client support.
If all you need is read the url you do not need to resort to third party libraries, java has built in support to retrieve urls.
import java.net.*;
import java.io.*;
public class URLConnectionReader {
public static void main(String[] args) throws Exception {
URL yahoo = new URL("http://www.yahoo.com/");
URLConnection yc = yahoo.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
If it was php, you could use cURL, but since it's java, you would use HttpURLConnection, as I just found out on this question:
cURL equivalent in JAVA
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class URLConetent{
public static void main(String[] args) {
URL url;
try {
// get URL content
String a="http://localhost:8080//TestWeb/index.jsp";
url = new URL(a);
URLConnection conn = url.openConnection();
// open the stream and put it into BufferedReader
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
while ((inputLine = br.readLine()) != null) {
System.out.println(inputLine);
}
br.close();
System.out.println("Done");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Categories

Resources