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 with the code:
Copy codeThe 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 );
The
Copy 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.