Java obtains the URL content

Source: Internet
Author: User

Java obtains the URL content. Here I only provide the get method. The post method is similar to other methods. The technical points are as follows.
1. Create httpurlconnection
2. Open the URL and create an inputstream
Third: Read data row by row (byte). If needed, convert the encoding and put it into a string.
Okay. Let's get started. Code Right: Copy code The Code is as follows: Public String geturlcontent (string path ){
String RTN = "";
Int C;
Try {
Java.net. url l_url = new java.net. URL (PATH );
Java.net. httpurlconnection l_connection = (java.net. httpurlconnection) l_url.openconnection ();
Rochelle connection.setrequestproperty ("User-Agent", "Mozilla/4.0 ");
Rochelle connection.connect ();
Inputstream l_urlstream = l_connection.getinputstream ();
While (C = l_urlstream.read ())! =-1 )){
Int all = l_urlstream.available ();
Byte [] B = new byte [all];
L_urlstream.read (B );
RTN + = new string (B, "UTF-8 ");
}
// Thread. Sleep (2000 );
L_urlstream.close ();
} Catch (exception e ){
E. printstacktrace ();
}
Return RTN;
}

Where
Rochelle connection.setrequestproperty ("User-Agent", "Mozilla/4.0 ");
This statement is required. Many servers directly send 403 requests without the User-Agent header.
Then inputstream is used. available () for one-time reading. The next caller of the input stream method can read (or skip) the number of bytes from the input stream without blocking. If necessary, you can sleep the thread a little later:
Thread. Sleep (2000 );
TheCopy codeThe Code is as follows: While (C = l_urlstream.read ())! =-1 )){
Int all = l_urlstream.available ();
Byte [] B = new byte [all];
L_urlstream.read (B );
RTN + = new string (B, "UTF-8 ");
}

It is a very important read process. C indicates the mark of the read stream. when it ends, it is-1, and then all is the maximum number of bytes available in this cycle, then read all available bytes to byte [] B, and then convert to the UTF-8 type of string, note, here you can write your own way, if read gb2312, it is necessary to write gb2312, if there is no omnipotent means, you can get the Meta and match it. Find a solution.

Related Article

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.