The following code can be used to get the response result of a link and to make a simple processing of the response result into a file.
ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.File;ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.OutputStreamWriter;ImportJava.net.URL;Importjava.net.URLConnection;Importjava.util.Date;Importjava.util.List;ImportJava.util.Map;Importjavax.print.attribute.standard.DateTimeAtCompleted; Public classGetResponse {/*** A request to send a GET method to a specified URL *@paramURL * URL to send request *@paramparam * Request parameters, request parameters should be in the form of name1=value1&name2=value2. * @returnThe response result of the remote resource represented by the URL*/ Public Staticstring sendget (string url, string param) {string result= ""; BufferedReader in=NULL; Try{String urlnamestring= URL + "?" +param; System.out.println ("Read response:" +urlnamestring); URL Realurl=NewURL (urlnamestring); //opening and linking between URLsURLConnection connection =realurl.openconnection (); //to set common request propertiesConnection.setrequestproperty ("Accept", "*/*"); Connection.setrequestproperty ("Connection", "keep-alive"); Connection.setrequestproperty ("User-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/44.0.2403.155 safari/537.36 "); //establish an actual connectionConnection.connect (); //Get all response header fieldsmap<string, list<string>> map =Connection.getheaderfields (); //iterate through all the response header fields for(String key:map.keySet ()) {System.out.println (key+ "--->" +Map.get (key)); } //defines the response of the BufferedReader input stream to read the URLin =NewBufferedReader (NewInputStreamReader (Connection.getinputstream ())); String Line; while(line = In.readline ())! =NULL) {result+=Line ; } } Catch(Exception e) {System.out.println ("Send GET request exception!" " +e); E.printstacktrace (); } //Use the finally block to close the input stream finally { Try { if(In! =NULL) {in.close (); } } Catch(Exception E2) {e2.printstacktrace (); } } returnresult; } Public Static voidMain (string[] args) {Date currenttime=NewDate (); String URL= "Http://xxx.com"; String result= ""; for(inti = 0; I < 10; i++) { if(Result.equals ("") ) {currenttime=NewDate (); Result= Sendget (URL, currenttime.gettime () + ""); }} System.out.println (Result); String[] Result2= Result.split ("="); System.out.println (result2[1]); System.out.println (); Try{File File=NewFile ("Goldprice.txt"); //If file doesnt exists, then create it if(!file.exists ()) {File.createnewfile (); } //true = Append fileFileWriter Filewritter =NewFileWriter (File.getname (),true); BufferedWriter Bufferwritter=NewBufferedWriter (Filewritter); Bufferwritter.write (CurrentTime+ Result2[2].replace ("|", "\ T")); Bufferwritter.newline (); Bufferwritter.close (); System.out.println ("Done"); } Catch(IOException e) {e.printstacktrace (); } }}
Save the above code in the. Java format. Under the Command window, run: Javac Getresponse.java to generate the. class file.
Create a new file, save the following copy to the file, and use the. bat suffix. When the bat file is executed, the status of execution is saved to the Main.log file.
Java GetResponse > Main.log
[Java] Gets the content of a site that is saved