URL openConnection Example

URL class openConnection example. This example shows you how to use openConnection method.


URL class openConnection example. public URLConnection openConnection(Proxy proxy) throws IOException Same as openConnection(), except that the connection will be made through the specified proxy; Protocol handlers that do not support proxing will ignore the proxy parameter and make a normal connection.

Here is the code
/*
 * @ # OpenConnection.java
 * A class repersenting use to openConnection method 
 * of URL class in java.net package
 * version 24 June 2008
 * author Rose India 
 */

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

public class OpenConnection1 {

    public static void main(String args[]) throws Exception {

        SocketAddress socketAddress = new InetSocketAddress("192.168.10.80"9090);
        Proxy proxy = new Proxy(Proxy.Type.HTTP, socketAddress);
        URL url = new URL("http://google.co.in");
        URLConnection connection = url.openConnection(proxy);
        BufferedReader br = new BufferedReader(
                new InputStreamReader(connection.getInputStream()));
        String inputLine;

        while ((inputLine = br.readLine()) != null) {
            System.out.println(inputLine);
        }
        br.close();
    }
}

Output
<html><head><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"><title>Google</title><style>body,td,a,p,.h{font-family:arial,sans-serif}.h{font-size:20px}.h{color:#3366cc}.q{color:#00c}.ts td{padding:0}.ts{border-collapse:collapse}.lnc:link,.lnc:visited{color:#00c}.pgtab,.pgtab:hover,.pgtabselected,.pgtabside{text-align:center;text-decoration:none;color:#00c;display:block;height:27px;float:left;overflow:hidden;background:url(/intl/ja/images/productlinktabs.png) no-repeat;padding-top:8px}.pgtab{width:130px;background-position:-274px 0}.pgtab:hover{width:130px;background-position:-144px 0}.pgtabselected{width:144px}.pgtabside{width:3px;background-position:-404px 0}.iconl{overflow:hidden;height:px;width:px;position:relative}#gbar{float:left;height:22px;padding-left:2px}.gbh,.gb2 div{border-top:1px solid #c9d7f1;font-size:0;height:0}.gbh{position:absolute;top:24px;width:100%}.gb2 div{margin:5px}#gbi{background:#fff;border:1px solid;border-color:#c9d7f1 #36c #36c #a2bae7;font-size:13px;top:24px;z-index:1000}#guser{padding-bottom:7px !important}#gbar,#guser{font-size:13px;padding-top:1px !important}@media all{.gb1,.gb3{height:22px;margin-right:.73em;vertical-align:top}.gb2 a,.gb2 b{display:block;padding:.2em .5em}}#gbi,.gb2{display:none;position:absolute;width:8em}.gb2{z-index:1001}#gbar a{color:#00c}.gb2 a,.gb3 a{text-decoration:none}#gbar .gb2 a:hover{background:#36c;color:#fff;display:block}</style><script>window.google={kEI:"oK5gSP_FNYzm6gPR1sm-Dg",kEXPI:"17259,17735",kHL:"en"};
function sf(){document.f.q.focus()}
window.gbar={};(function(){var c=window.gbar,e,g,h;c.qs=function(a){var d=window.encodeURIComponent&&(document.forms[0].q||"").value;if(d)a.href=a.href.replace(/([?&])q=[^&]*|$/,function(f,b){return(b||"&")+"q="+encodeURIComponent(d)})};function l(a,d,f){a.display=h?"none":"block";a.left=d+"px";a.top=f+"px"}c.tg=function(a){var d=0,f=0,b,m=0,n,j=window.navExtra,k,i=document;g=g||i.getElementById("gbar").getElementsByTagName("span");(a||window.event).cancelBubble=!m;if(!e){e=i.createElement(Array.every||window.createPopup?"iframe":"DIV");e.frameBorder="0";e.scrolling="no";e.src="#";g[7].parentNode.appendChild(e).id="gbi";if(j&&g[7])for(n in j){k=i.createElement("span");k.appendChild(j[n]);g[7].parentNode.insertBefore(k,g[7]).className="gb2"}i.onclick=c.close}while(b=g[++m]){if(f){l(b.style,f+1,d+25);d+=b.firstChild.tagName=="DIV"?9:20}if(b.className=="gb3"){do f+=b.offsetLeft;while(b=b.offsetParent)}}e.style.height=d+"px";l(e.style,f,24);h=!h};c.close=function(a){h&&c.tg(a)}})();</script></head><body bgcolor=#ffffff text=#000000 link=#0000cc vlink=#551a8b alink=#ff0000 onload="sf();if(document.images){new Image().src='/images/nav_logo3.png'}" topmargin=3 marginheight=3><div id=gbar><nobr><span class=gb1><b>Web</b></span> <span class=gb1><a href="http://images.google.co.in/imghp?hl=en&tab=wi" onclick=gbar.qs(this)>Images</a></span> <span class=gb1><a href="http://news.google.co.in/nwshp?hl=en&tab=wn" onclick=gbar.qs(this)>News</a></span> <span class=gb1><a href="http://www.orkut.com/Home.aspx?hl=en&tab=w0" onclick=gbar.qs(this)>Orkut</a></span> <span class=gb1><a href="http://groups.google.co.in/grphp?hl=en&tab=wg" onclick=gbar.qs(this)>Groups</a></span> <span class=gb1><a href="http://mail.google.com/mail/?hl=en&tab=wm">Gmail</a></span> <span class=gb3><a href="http://www.google.co.in/intl/en/options/" onclick="this.blur();gbar.tg(event);return !1"><u>more</u> <small>&#9660;</small></a></span> <span class=gb2><a href="http://books.google.co.in/bkshp?hl=en&tab=wp" onclick=gbar.qs(this)>Books</a></span> <span class=gb2><a href="http://scholar.google.co.in/schhp?hl=en&tab=ws" onclick=gbar.qs(this)>Scholar</a></span> <span class=gb2><a href="http://blogsearch.google.co.in/?hl=en&tab=wb" onclick=gbar.qs(this)>Blogs</a></span> <span class=gb2><div></div></a></span> <span class=gb2><a href="http://www.google.com/calendar/render?hl=en&tab=wc">Calendar</a></span> <span class=gb2><a href="http://picasaweb.google.co.in/home?hl=en&tab=wq" onclick=gbar.qs(this)>Photos</a></span> <span class=gb2><a href="http://docs.google.com/?hl=en&tab=wo">Documents</a></span> <span class=gb2><a href="http://www.google.co.in/reader/view/?hl=en&tab=wy">Reader</a></span> <span class=gb2><div></div></a></span> <span class=gb2><a href="http://www.google.co.in/intl/en/options/">even more &raquo;</a></span> </nobr></div><div class=gbh style=left:0></div><div class=gbh style=right:0></div><div align=right id=guser style="font-size:84%;padding:0 0 4px" width=100%><nobr><a href="/url?sa=p&pref=ig&pval=3&q=http://www.google.co.in/ig%3Fhl%3Den%26source%3Diglk&usg=AFQjCNHe9Dv_h1zYL1VKlS7wKo_0Kg38dg">iGoogle</a> | <a href="https://www.google.com/accounts/Login?continue=http://www.google.co.in/&hl=en">Sign in</a></nobr></div><center><br clear=all id=lgpd><table cellpadding=0 cellspacing=0 border=0><tr><td align=right valign=bottom><img src=images/hp0.gif width=158 height=78 alt="Google"></td><td valign=bottom><img src=images/hp1.gif width=50 height=78 alt=""></td><td valign=bottom><img src=images/hp2.gif width=68 height=78 alt=""></td></tr><tr><td class=h align=right valign=top><b></b></td><td valign=top><img src=images/hp3.gif width=50 height=32 alt=""></td><td valign=top class=h><font color=#666666 style=font-size:16px><b>India</b></font></td></tr></table><br><form action="/search" name=f><table cellpadding=0 cellspacing=0><tr valign=top><td width=25%>&nbsp;</td><td align=center nowrap><input name=hl type=hidden value=en><input type=hidden name=ie value="ISO-8859-1"><input maxlength=2048 name=q size=55 title="Google Search" value=""><br><input name=btnG type=submit value="Google Search"><input name=btnI type=submit value="I'm Feeling Lucky"></td><td nowrap width=25%><font size=-2>&nbsp;&nbsp;<a href=/advanced_search?hl=en>Advanced Search</a><br>&nbsp;&nbsp;<a href=/preferences?hl=en>Preferences</a><br>&nbsp;&nbsp;<a href=/language_tools?hl=en>Language Tools</a></font></td></tr><tr><td align=center colspan=3><font size=-1><span style="text-align:left">Search: <input id=all type=radio name=meta value="" checked><label for=all> the web </label><input id=cty type=radio name=meta value="cr=countryIN"><label for=cty> pages from India </label></span></font></td></tr></table></form><br><font size=-1>Google.co.in offered in: <a href="http://www.google.co.in/setprefs?sig=0_-Idq-YxSv6FfNHJE3Ziuz2y0f2Y=&hl=hi">Hindi</a>  <a href="http://www.google.co.in/setprefs?sig=0_-Idq-YxSv6FfNHJE3Ziuz2y0f2Y=&hl=bn">Bengali</a>  <a href="http://www.google.co.in/setprefs?sig=0_-Idq-YxSv6FfNHJE3Ziuz2y0f2Y=&hl=te">Telugu</a>  <a href="http://www.google.co.in/setprefs?sig=0_-Idq-YxSv6FfNHJE3Ziuz2y0f2Y=&hl=mr">Marathi</a>  <a href="http://www.google.co.in/setprefs?sig=0_-Idq-YxSv6FfNHJE3Ziuz2y0f2Y=&hl=ta">Tamil</a> </font><br><br><br><font size=-1><a href="/intl/en/ads/">Advertising&nbsp;Programs</a> - <a href="/intl/en/about.html">About Google</a> - <a href=/intl/en/jobs/>We're Hiring</a> - <a href=http://www.google.com/ncr>Go to Google.com</a></font><p><font size=-2>&copy;2008 Google</font></p></center></body></html>

Post Comment
Name:
E-mail:
Contact no :
Comments:
  Refresh Image
Verify Image:
 
 
Your Comment's
 
 
 

HOME | COPYRIGHT | CONTACT US