Java Crawl Web content implementation code _java

Source: Internet
Author: User
Tags readline stringbuffer

Copy Code code as follows:

Package test;

Import Java.io.BufferedReader;

Import java.io.IOException;

Import Java.io.InputStream;

Import Java.io.InputStreamReader;

Import Java.net.Authenticator;

Import java.net.HttpURLConnection;

Import java.net.PasswordAuthentication;

Import Java.net.URL;

Import java.net.URLConnection;

Import java.util.Properties;

public class Urltest {

A public method that returns a string, and an error returns an "Error open URL"

public static string GetContent (string strurl) {

try {

URL url = new URL (strurl);

BufferedReader br = new BufferedReader (new InputStreamReader (URL

. OpenStream ());

String s = "";

StringBuffer sb = new StringBuffer ("");

while ((s = br.readline ())!= null) {

Sb.append (S + "/r/n");

}

Br.close ();

return sb.tostring ();

catch (Exception e) {

Return "Error open URL:" + strurl;

}

}

public static void Initproxy (string host, int port, final String username,

Final String password) {

Authenticator.setdefault (New Authenticator () {

Protected Passwordauthentication getpasswordauthentication () {

Return to New Passwordauthentication (username,

New String (password). ToCharArray ());

}

});

System.setproperty ("Http.proxytype", "4");

System.setproperty ("Http.proxyport", integer.tostring (port));

System.setproperty ("Http.proxyhost", host);

System.setproperty ("Http.proxyset", "true");

}

public static void Main (string[] args) throws IOException {

String url = "Http://www.jb51.net";

String proxy = "http://192.168.22.81";

int port = 80;

String username = "username";

String password = "password";

String curline = "";

String content = "";

URL server = new URL (URL);

Initproxy (proxy, port, username, password);

HttpURLConnection connection = (httpurlconnection) server

. OpenConnection ();

Connection.connect ();

InputStream is = Connection.getinputstream ();

BufferedReader reader = new BufferedReader (new

InputStreamReader (IS));

while ((CurLine = Reader.readline ())!= null) {

Content = content + curline+ "/r/n";

}

System.out.println ("content=" + content);

Is.close ();

System.out.println (getcontent (URL));

}

}

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.