Click here to Skip to main content
15,888,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Using this code to access html from a url within java app:

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 Test {
    public static void main(String[] args) {
        try {
            // get URL content
            
            String a = "https://www.example.com";
            URL url = new URL(a);
            URLConnection conn = url.openConnection();
            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();
        }
    }
}



but, when saving t5his code in .html format and opening it with a browser, some content like images dont apear there. does the code missing something?

What I have tried:

it seems like to be the simpleset code to parse the html.
Posted
Updated 13-Feb-22 22:21pm
v3
Comments
Richard MacCutchan 14-Feb-22 3:49am    
Without seeing what you have saved it is impossible to guess.
T1xT 14-Feb-22 4:03am    
website : https://www.playok.com/en/spades/

parsed html:


|| 




<title>PlayOK - Play Spades Online Free

window.cklang = "en";

if (/Windows NT|X11/.test(navigator.userAgent))
document.documentElement.className += " win";





.ltbg { background: #f4f8ff; color: #234; }
.dark .ltbg { background: #282828; color: inherit; }






<a href="/"></a>
<a href="/">online games</a> | <a href="/en/tournaments/">tournaments</a>12091 players online


<h1>spades online free</h1>


var wsz = 'width=980,height=580';
var wrl = 0;
function gstart(gmid, nwexpl) {  // desk only
  var nwopt = document.getElementById("nwin");
  var nw = nwexpl || (nwopt && nwopt.checked);
  document.cookie = 'kbexp=0;path=/;';
  document.cookie = 'kbeta=' + gmid + (nw ? '/1' : '') + ';path=/;';
  var iplk = (navigator.platform === "MacIntel" && typeof navigator.standalone !== "undefined");
  var whmin = Math.min(screen.width, screen.height);
  var mdsk = whmin > 0 && whmin < 480;  // 476
  if (wrl || (window.name && window.name == gmid) || (mdsk && !nw) || iplk) {
    window.location.reload(); return;
  }
  var wo = window.open(window.location.href.replace('#_=_',''), gmid,
    nw ? 'resizable=1,scrollbars=1,top=0,left=0,' + wsz : '');
  if (nw && wo) wo.focus();
}







<h2 class="ttup nu lc"><a href="">spades multiplayer game</a></h2>

log in â–¾guest â–¸





username, password







<a class="lbut bxpad ttup" href="/en/register.phtml?t=sp">sign up for free</a>

<a href="/en/pass.phtml">password</a>

facebook
<span id="acimg">...</span>




classic trick-taking card game for four players in two partnerships; 52-card deck; spade suit trump; goal to take as many tricks as declared in bidding; score 500 to win; free spades card game online, internet spades



<ins class="adsbygoogle" id="abp" data-ad-client="ca-pub-6421596181997270" data-ad-slot="3943951797"></ins>

(adsbygoogle = window.adsbygoogle || []).push({});

Features: live opponents, game rooms, rankings, extensive stats, user profiles, contact lists, private messaging, game records, support for mobile devices.
free online games, play against live opponents
<a target="_blank" href="https://www.pagat.com/boston/spades.html">game rules</a>














document.cookie = "__cxtst=1;path=/";
var cok = document.cookie.indexOf("__cxtst=1") > -1;
document.cookie = "__cxtst=;path=/;expires=Thu, 01 Jan 1970 00:00:01 GMT";
if (!cok)
  document.getElementById("cc").value = 1;


if (window.location.hash == '#_=_' && history.replaceState)
  history.replaceState(null, null, window.location.href.split('#')[0]);



if (top!=self) top.location.href=self.location.href;



<a href="/en/feedback/">feedback</a> | <a href="/en/privacy.phtml">privacy</a> | <a href="/en/contact.phtml">contact</a>   <a href="">English â–¾</a>

<a href="/bg/spades/">ذ‘رٹذ»ذ³ذ°ر€رپذ؛ذ¸</a>
<a href="/ca/piques/">Catalأ </a>
<a href="/cs/piky/">ؤچeskأ½</a>
<a href="/da/spar/">dansk</a>
<a href="/de/spades/">Deutsch</a>
<a href="/el/spades/">خ•خ»خ»خ·خ½خ¹خ؛خ¬</a>
<a href="/et/speades/">eesti</a>
<a href="/en/spades/">English</a>
<a href="/es/picas/">espaأ±ol</a>
<a href="/fr/spades/">franأ§ais</a>
<a href="/hr/spades/">hrvatski</a>
<a href="/it/picche/">italiano</a>
<a href="/lv/piki/">latvieإ،u</a>
<a href="/lt/vynai/">Lietuviإ³</a>
<a href="/hu/pikk/">magyar</a>
<a href="/nl/schoppen/">Nederlands</a>
<a href="/no/spar/
Richard MacCutchan 14-Feb-22 4:17am    
There is quite a bit of the HTML missing from that text: the DOCTYPE header and the HTML head and body tags.

1 solution

You need to study the documentation for the URLConnection (Java Platform SE 7 )[^] class. It explains there how to access the complete details of the HTML.
 
Share this answer
 
Comments
T1xT 14-Feb-22 4:24am    
thx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900