Use of URLs in Java

Source: Internet
Author: User

/**
*
*/
Package com. duoguo. url;
Import java. io. BufferedReader;
Import java. io. IOException;
Import java. io. InputStreamReader;
Import java.net. MalformedURLException;
Import java.net. URL;
Import java.net. URLConnection;
/**
* @ Author shyboy (chao.shen@duoguo.cn)
*
*/
Public class URLClient {
StringBuilder sb = new StringBuilder ();
/**
* Connect to the server and request the specified document
*
* @ Param urlString
*: Document address
* @ Return: String
*/
Public String getDocumentAt (String urlString ){
Try {
URL url = new URL (urlString); // generate a url object
URLConnection urlConnection = url. openConnection (); // open the url Connection
BufferedReader br = new BufferedReader (new InputStreamReader (
UrlConnection. getInputStream ()));
String line = null;
While (line = br. readLine ())! = Null ){
Sb. append (line + "\ n ");
}
} Catch (MalformedURLException e ){
System. out. println ("cannot connect to URL:" + urlString );
E. printStackTrace ();
} Catch (IOException e ){
System. out. println ("connecting to URL throws exception information:" + urlString );
E. printStackTrace ();
}
Return sb. toString ();
}
/**
* @ Param args
*/
Public static void main (String [] args ){
URLClient client = new URLClient ();
String url = client. getDocumentAt ("http://www.baidu.com ");
System. out. println (url );
}
}

Author: "shenjichao2009"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.