Package com.java.base;
Import Java.io.BufferedReader;
Import Java.io.InputStreamReader;
Import java.net.HttpURLConnection;
Import Java.net.URL;
public class Javahttphandle {
/**
* Request to send a GET method to the specified URL
* @param URL
* Send the requested URL
* @param param
* Request parameters, request parameters should be in the form of name1=value1&name2=value2.
* @return The response result of the remote resource represented by the URL
*/
public static void Main (string[] args) {
Javahttphandle http=new Javahttphandle ();
Http.sendget ("http://www.baidu.com"); }
public void Sendget (String urladderss) {
try{
Establish a reference to the HttpURLConnection class
HttpURLConnection Urlconnection=null;
To create an object of the URL class
URL url=new url (urladderss);
Instantiate the URLConnection, and then cast the url.openconnection () casting
Link between open and URL
urlconnection= (HttpURLConnection) url.openconnection ();
Set the connection time-out to 5000ms
Urlconnection.setconnecttimeout (5000);
Set the wait time to 5000ms
Urlconnection.setreadtimeout (5000);
Whether the connection uses the cache, set to False
Urlconnection.setusecaches (FALSE);
Establish the actual connection urlconnection.connect ();
Define BufferedReader to get response content
BufferedReader bf=new BufferedReader (New InputStreamReader (Urlconnection.getinputstream (), "UTF-8"));
String Line=bf.readline ();
while (Line!=null) {System.out.println (line); Line=bf.readline (); } }
catch (Exception e) {e.printstacktrace (); } } }
Java sends a GET request