Java code to view Source of a web page..


//  This code is working perfectly.......

import java.io.*;
import java.net.*;

class SourceViewer{
  public static void main (String[] args) throws IOException{
  System.out.print("Enter url of local for viewing html source code: ");
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  String url = br.readLine();
  try{
  URL u = new URL(url);
  HttpURLConnection uc = (HttpURLConnection) u.openConnection();
  int code = uc.getResponseCode();
  String response = uc.getResponseMessage();
  System.out.println("HTTP/1.x " + code + " " + response);
  for(int j = 1; ; j++){
  String header = uc.getHeaderField(j);
  String key = uc.getHeaderFieldKey(j);
  if(header == null || key == null)
  break;
  System.out.println(uc.getHeaderFieldKey(j) + ": " + header);
  }
  InputStream in = new BufferedInputStream(uc.getInputStream());
  Reader r = new InputStreamReader(in);
  int c;
  while((c = r.read()) != -1){
  System.out.print((char)c);
  }
  }
  catch(MalformedURLException ex){
  System.err.println(url + " is not a valid URL.");
  }
  catch(IOException ie){
  System.out.println("Input/Output Error: " + ie.getMessage());
  }
  }
}


Execution :

java SourceViewer

Enter url of local for viewing html source code: https://www.google.co.in
HTTP/1.x 200 OK
Date: Fri, 05 Oct 2012 05:37:38 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Set-Cookie: PREF=ID=45eb1211104a5684:FF=0:TM=1349415458:LM=1349415458:S=vQJMFdQP
SSmKCEOd; expires=Sun, 05-Oct-2014 05:37:38 GMT; path=/; domain=.google.co.in
Set-Cookie: NID=64=CYkropT3HUVUE-Rm3AFo_jZmeKYCO6-FCoZ8jUusHp1QXK5eVz8u--Egtc7zD
cCXrSAHrsVgBjEdmdH-mn6BwkpTxVQAjJKq2VB9QTAa-5gtUVQFuzUGPZjE37K7eFSw; expires=Sat
, 06-Apr-2013 05:37:38 GMT; path=/; domain=.google.co.in; HttpOnly
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bi
n/answer.py?hl=en&answer=151657 for more info."
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Transfer-Encoding: chunked
<!doctype html><html itemscope="itemscope" itemtype="http://schema.org/WebPage">
<head><meta itemprop="image" content="/images/google_favicon_128.png"><title>Goo
gle</title><script>window.google={kEI:"InJuUJ_sMpGnrAfGjoCoCA",getEI:function(a)
{var b;while(a&&!(a.getAttribute&&(b=a.getAttribute("eid"))))a=a.parentNode;retu
rn b||google.kEI},https:function(){return window.location.protocol=="https:"},kE
XPI:"17259,39523,39977,4000116,4000354,4000472,4000553,4000624,4000648,4000723,4
000743,4000769,4000833,4000955,4001007,4001013,4001128,4001131,4001145,4001174,4
001192,4001267",kCSI:{e:"17259,39523,39977,4000116,4000354,4000472,4000553,40006
24,4000648,4000723,4000743,4000769,4000833,4000955,4001007,4001013,4001128,40011
31,4001145,4001174,4001192,4001267",ei:"InJuUJ_sMpGnrAfGjoCoCA"},authuser:0,
ml:function(){},kHL:"en",time:function(){return(new Date).getTime()},log:functio
n(a,b,c,e){var d=new Image,h=google,i=h.lc,f=h.li,j="";d.onerror=(d.onload=(d.on
abort=function(){delete i[f]}));i[f]=d;if(!c&&b.search("&ei=")==-1)j="&ei="+goog
le.getEI(e);var g=c||"/gen_204?atyp=i&ct="+a+"&cad="+b+j+"&zx="+google.time();
var k=/^http:/i;if(k.test(g)&&google.https()){google.ml(new Error("GLMM"),false,
{src:g});delete i[f];return}d.src=g;h.li=f+1},lc:[],li:0,Toolbelt:{},y:{},x:func
tion(a,b){google.y[a.id]=[a,b];return false}};

window.google.sn="webhp";window.google.timers={};window.google.startTick=functio
n(a,b){window.google.timers[a]={t:{start:(new Date).getTime()},bfr:!(!b)}};windo
w.google.tick=function(a,b,c){if(!window.google.timers[a])google.startTick(a);wi
ndow.google.timers[a].t[b]=c||(new Date).getTime()};google.startTick("load",true
);try{}catch(u){}
var _gjwl=location;function _gjuc(){var e=_gjwl.href.indexOf("#");if(e>=0){var a
=_gjwl.href.substring(e);if(a.indexOf("&q=")>0||a.indexOf("#q=")>=0){a=a.substri
ng(1);if(a.indexOf("#")==-1){for(var c=0;c<a.length;){var d=c;if(a.charAt(d)=="&
")++d;var b=a.indexOf("&",d);if(b==-1)b=a.length;var f=a.substring(d,b);if(f.ind
exOf("fp=")==0){a=a.substring(0,c)+a.substring(b,a.length);b=c}else if(f=="cad=h
")return 0;c=b}_gjwl.href="/search?"+a+"&cad=h";return 1}}}return 0}function _gj
p(){!(window._gjwl.hash&&
window._gjuc())&&setTimeout(_gjp,500)};
window._gjp&&_gjp();</script><style>#gbar,#guser{font-size:13px;padding-top:1px
!important;}#gbar{height:22px}#guser{padding-bottom:7px !important;text-align:ri
ght}.gbh,.gbd{border-top:1px solid #c9d7f1;font-size:1px}.gbh{height:0;position:
absolute;top:24px;width:100%}@media all{.gb1{height:22;margin-right:.5em;vertica
l-align:top}#gbar{float:left}}a.gb1,a.gb4{text-decoration:underline !important}a
.gb1,a.gb4{color:#00c !important}</style><style id="gstyle">body{margin:0;overfl
ow-y:scroll}#gog{padding:3px 8px 0}td{line-height:.8em}.gac_m td{line-height:17p
x}form{margin-bottom:20px}body,td,a,p,.h{font-family:arial,sans-serif}.h{color:#
36c;font-size:20px}.q{color:#00c}.ts td{padding:0}.ts{border-collapse:collapse}e
m{font-weight:bold;font-style:normal}.lst{height:25px;width:496px}.gsfi,.lst{fon
t:18px arial,sans-serif}.gsfs{font:17px arial,sans-serif}.ds{display:-moz-inline
-box;display:inline-block;margin:3px 0 4px;margin-left:4px}input{font-family:inh
erit}a.gb1,a.gb2,a.gb3,a.gb4{color:#11c !important}body{background:#fff;color:bl
ack}a{color:#11c;text-decoration:none}a:hover,a:active{text-decoration:underline
}.fl a{color:#36c}a:visited{color:#551a8b}a.gb1,a.gb4{text-decoration:underline}
a.gb3:hover{text-decoration:none}#ghead a.gb2:hover{color:#fff!important}.sblc{p
adding-top:5px}.sblc a{display:block;margin:2px 0;margin-left:13px;font-size:11p
x;}.lsbb{background:#eee;border:solid 1px;border-color:#ccc #999 #999 #ccc;heigh
t:30px;display:block}.ftl,#fll a{display:inline-block;margin:0 12px}.lsb{backgro
und:url(/images/srpr/nav_logo80.png) 0 -258px repeat-x;border:none;color:#000;cu
rsor:pointer;height:30px;margin:0;outline:0;font:15px arial,sans-serif;vertical-
align:top}.lsb:active{background:#ccc}.lst:focus{outline:none}#addlang a{padding
:0 3px}.gac_v div{display:none}.gac_v .gac_v2,.gac_bt{display:block!important}ta
ble.gssb_c{z-index:986  }.nbcl{background:url(/images/srpr/nav_logo80.png) no-re
peat ;height:px;width:px}</style><script>window.rwt=function(a,f,g,l,m,h,c,n,i){
try{if(a===window){a=window.event.srcElement;while(a){if(a.href)break;a=a.parent
Node}}var b=encodeURIComponent||escape,d;d=a.getAttribute("href");if(c&&c.substr
ing(0,6)!="&sig2=")c="&sig2="+c;var o=["/url?sa=t","","&cd=",b(m),i?"&authuser="
+b(i):"",google.j&&google.j.pf?"&sqi=2":"","&ved=",b(n),"&url=",b(d).replace(/\+
/g,"%2B"),"&ei=","InJuUJ_sMpGnrAfGjoCoCA",h?"&usg="+h:"",c].join("");a.href=o;a.
onmousedown=""}catch(p){}return true};
</script> </head><body dir="ltr" bgcolor="#fff"><script>(function(){var src='/im
ages/srpr/nav_logo80.png';var iesg=false;document.body.onload = function(){windo
w.n && window.n();if (document.images){new Image().src=src;}
if (!iesg){document.f&&document.f.q.focus();document.gbqf&&document.gbqf.q.focus
();}
}
})();</script><textarea id="csi" style="display:none"></textarea><div id="mngb">
<div id=gbar><nobr><b class=gb1>Search</b> <a class=gb1 href="https://www.google
.co.in/imghp?hl=en&tab=wi">Images</a> <a class=gb1 href="http://maps.google.co.i
n/maps?hl=en&tab=wl">Maps</a> <a class=gb1 href="http://news.google.co.in/nwshp?
hl=en&tab=wn">News</a> <a class=gb1 href="http://www.orkut.com/Main?tab=w0#Home"
>Orkut</a> <a class=gb1 href="http://translate.google.co.in/?hl=en&tab=wT">Trans
late</a> <a class=gb1 href="https://mail.google.com/mail/?tab=wm">Gmail</a> <a c
lass=gb1 style="text-decoration:none" href="http://www.google.co.in/intl/en/opti
ons/"><u>More</u> &raquo;</a></nobr></div><div id=guser width=100%><nobr><span i
d=gbn class=gbi></span><span id=gbf class=gbf></span><span id=gbe></span><a href
="http://www.google.co.in/history/optout?hl=en" class=gb4>Web History</a> | <a
href="/preferences?hl=en" class=gb4>Settings</a> | <a target=_top id=gb_70 href=
"https://accounts.google.com/ServiceLogin?hl=en&continue=https://www.google.co.i
n/" class=gb4>Sign in</a></nobr></div><div class=gbh style=left:0></div><div cla
ss=gbh style=right:0></div></div><center><br clear="all" id="lgpd"><div id="lga"
><div style="padding:28px 0 3px"><div dir="ltr" title="Google" align="left" id="
hplogo" onload="window.lol&&lol()" style="height:110px;width:276px;background:ur
l(/images/srpr/logo1w.png) no-repeat"><div nowrap="nowrap" style="color:#777;fon
t-size:16px;font-weight:bold;position:relative;left:214px;top:70px">India</div><
/div></div><br></div><form action="/search" name="f"><table cellpadding="0" cell
spacing="0"><tr valign="top"><td width="25%">&nbsp;</td><td align="center" nowra
p="nowrap"><input name="ie" value="ISO-8859-1" type="hidden"><input value="en-IN
" name="hl" type="hidden"><input name="source" type="hidden" value="hp"><div cla
ss="ds" style="height:32px;margin:4px 0"><input autocomplete="off" class="lst" v
alue="" title="Google Search" maxlength="2048" name="q" size="57" style="color:#
000;margin:0;padding:5px 8px 0 6px;vertical-align:top"></div><br style="line-hei
ght:0"><span class="ds"><span class="lsbb"><input class="lsb" value="Google Sear
ch" name="btnG" type="submit"></span></span><span class="ds"><span class="lsbb">
<input class="lsb" value="I'm Feeling Lucky" name="btnI" type="submit" onclick="
if(this.form.q.value)this.checked=1; else top.location='/doodles/'"></span></spa
n></td><td class="fl sblc" align="left" nowrap="nowrap" width="25%"><a href="/ad
vanced_search?hl=en-IN&amp;authuser=0">Advanced search</a><a href="/language_too
ls?hl=en-IN&amp;authuser=0">Language tools</a></td></tr></table><input type="hid
den" id="gbv" name="gbv" value="1"></form><div id="gac_scont"></div><div style="
font-size:83%;min-height:3.5em"><br><div id=als><font size=-1 id=addlang>Google.
co.in offered in: <a href="https://www.google.co.in/setprefs?sig=0_jqiOWcc0yb1at
Da8S_55jZMYMlQ%3D&amp;hl=hi&amp;source=homepage">Hindi</a> <a href="https://www.
google.co.in/setprefs?sig=0_jqiOWcc0yb1atDa8S_55jZMYMlQ%3D&amp;hl=bn&amp;source=
homepage">Bengali</a> <a href="https://www.google.co.in/setprefs?sig=0_jqiOWcc0y
b1atDa8S_55jZMYMlQ%3D&amp;hl=te&amp;source=homepage">Telugu</a> <a href="https:/
/www.google.co.in/setprefs?sig=0_jqiOWcc0yb1atDa8S_55jZMYMlQ%3D&amp;hl=mr&amp;so
urce=homepage">Marathi</a> <a href="https://www.google.co.in/setprefs?sig=0_jqiO
Wcc0yb1atDa8S_55jZMYMlQ%3D&amp;hl=ta&amp;source=homepage">Tamil</a> <a href="htt
ps://www.google.co.in/setprefs?sig=0_jqiOWcc0yb1atDa8S_55jZMYMlQ%3D&amp;hl=gu&am
p;source=homepage">Gujarati</a> <a href="https://www.google.co.in/setprefs?sig=0
_jqiOWcc0yb1atDa8S_55jZMYMlQ%3D&amp;hl=kn&amp;source=homepage">Kannada</a> <a hr
ef="https://www.google.co.in/setprefs?sig=0_jqiOWcc0yb1atDa8S_55jZMYMlQ%3D&amp;h
l=ml&amp;source=homepage">Malayalam</a> <a href="https://www.google.co.in/setpre
fs?sig=0_jqiOWcc0yb1atDa8S_55jZMYMlQ%3D&amp;hl=pa&amp;source=homepage">Punjabi</
a></font><br><br></div></div><span id="footer"><div style="font-size:10pt"><div
id="fll" style="margin:19px auto;text-align:center"><a href="/intl/en/ads/">Adve
rtising&nbsp;Programs</a><a href="http://www.google.co.in/services/">Business So
lutions</a><a href="https://plus.google.com/104205742743787718296" rel="publishe
r">+Google</a><a href="/intl/en/about.html">About Google</a><a id="fehl" href="h
ttps://www.google.co.in/setprefdomain?prefdom=US&amp;sig=0_BCDWLShslu8pXwLkwSZQJ
XrPj9c%3D">Google.com</a></div></div><p style="color:#767676;font-size:8pt">&cop
y; 2012 - <a href="/intl/en/policies/">Privacy & Terms</a></p></span></center><d
iv id=xjsd></div><div id=xjsi><script>if(google.y)google.y.first=[];(function(){

var c,d,e=false;function f(a){var b={_sn:a?"FAILURE":"FALLBACK",_pu:c,_fu:d},h=g
oogle.ml(new Error("pml"),false,b,true);google.log(0,"",h)}function g(){if(!goog
le.pml)f(true)}function i(a){window.setTimeout(function(){var b=document.createE
lement("script");b.src=a;document.getElementById("xjsd").appendChild(b)},0)}func
tion j(){if(!e&&!google.pml){e=
true;f();i(d,g)}}google.dljp=function(a,b){c=a;d=b;if(!google.xjsi){google.xjsu=
a;i(c,j)}};google.dlj=i;
})();
if(!google.xjs){google.dstr=[];google.rein=[];window._=window._||{};window._._Du
mpException=function(e){throw e};if(google.timers&&google.timers.load.t){google.
timers.load.t.xjsls=new Date().getTime();}google.dljp('/xjs/_/js/hp/hp,sb_he,pcc
/rt\x3dj/ver\x3dHuLohKHDEuE.en_US./d\x3d1/sv\x3d1/rs\x3dAItRSTM1OSb3H-_WPUgko92j
Vy2HvAIQQw','/xjs/_/js/hp/hp,sb_he,pcc/rt\x3dj/ver\x3dHuLohKHDEuE.en_US./d\x3d1/
sv\x3d1/rs\x3dAItRSTM1OSb3H-_WPUgko92jVy2HvAIQQw');google.xjs=1;}google.pmc={14:
{},10:{"agen":false,"cgen":true,"client":"heirloom-hp","dh":true,"ds":"","eqch":
true,"fl":true,"host":"google.co.in","jsonp":true,"msgs":{"lcky":"I\u0026#39;m F
eeling Lucky","lml":"Learn more","oskt":"Input tools","psrc":"This search was re
moved from your \u003Ca href=\"/history\"\u003EWeb History\u003C/a\u003E","psrl"
:"Remove","sbit":"Search by image","srch":"Google Search"},"ovr":{"ms":1},"pq":"
","qcpw":false,"scd":10,"sce":5,"stok":"c4DrE3PRJ7dHOBJGv8GK4-3lysk"},24:{}};goo
gle.y.first.push(function(){if(google.med){google.med('init');google.initHistory
();google.med('history');}google.History&&google.History.initialize('/');google.
hs&&google.hs.init&&google.hs.init()});if(google.j&&google.j.en&&google.j.xi){wi
ndow.setTimeout(google.j.xi,0);}</script></div><script>(function(){
var b,d,e,f;function g(a,c){if(a.removeEventListener){a.removeEventListener("loa
d",c,false);a.removeEventListener("error",c,false)}else{a.detachEvent("onload",c
);a.detachEvent("onerror",c)}}function h(a){f=(new Date).getTime();++d;a=a||wind
ow.event;var c=a.target||a.srcElement;g(c,h)}var i=document.getElementsByTagName
("img");b=i.length;d=0;for(var j=0,k;j<b;++j){k=i[j];if(k.complete||typeof k.src
!="string"||!k.src)++d;else if(k.addEventListener){k.addEventListener("load",h,f
alse);k.addEventListener("error",
h,false)}else{k.attachEvent("onload",h);k.attachEvent("onerror",h)}}e=b-d;functi
on l(){if(!google.timers.load.t)return;google.timers.load.t.ol=(new Date).getTim
e();google.timers.load.t.iml=f;google.kCSI.imc=d;google.kCSI.imn=b;google.kCSI.i
mp=e;if(google.stt!==undefined)google.kCSI.stt=google.stt;google.csiReport&&goog
le.csiReport()}if(window.addEventListener)window.addEventListener("load",
l,false);else if(window.attachEvent)window.attachEvent("onload",l);google.timers
.load.t.prt=(f=(new Date).getTime());
})();
</script></body></html>

Comments