Send a GET request and POST request to the Web site, and obtain the corresponding data example from the Web site.
Package cn. internet. demo; import java. io. bufferedReader; import java. io. inputStreamReader; import java. io. printWriter; import java.net. URL; import java.net. URLConnection; import java. util. list; import java. util. map; public class GetPostTest {/*** send a GET request to a specified URL */public static String sendGet (String url, String param) {String result = ""; string urlName = url + "? "+ Param; try {URL realUrl = new URL (urlName); // open the URL Connection URLConnection conn = realUrl. openConnection (); // set the common request attribute conn. setRequestProperty ("accept", "*/*"); conn. setRequestProperty ("connection", "Keep-Alive"); conn. setRequestProperty ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); // create the actual connection conn. connect (); System. out. println ("file size:" + conn. getContentLength (); // obtain all the corresponding header fields Map <String, Lis T <String> map = conn. getHeaderFields (); // traverses all corresponding header fields for (String key: map. keySet () {System. out. println (key + "--->" + map. get (key);} // defines the corresponding try (BufferedReader in = new BufferedReader (new InputStreamReader (conn. getInputStream (), "UTF-8") {String line; while (line = in. readLine ())! = Null) {result + = "\ n" + line ;}} catch (Exception e) {System. out. println ("an Exception occurred when sending a Get request! "); E. printStackTrace ();} return result;} public static String sendPost (String url, String param) {String result = ""; try {URL realUrl = new URL (url ); // open the URL Connection conn = realUrl. openConnection (); // set the common request attribute conn. setRequestProperty ("accept", "*/*"); conn. setRequestProperty ("connection", "Keep-Alive"); conn. setRequestProperty ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); // send The following two rows of conn must be set for the POST request. setDoOutput (true); conn. setDoInput (true); // get the output stream try (PrintWriter out = new PrintWriter (conn. getOutputStream () {out. print (param); // send the request parameter out. flush (); // flush buffer of the output stream} // defines the try (BufferedReader in = new BufferedReader (new InputStreamReader (conn. getInputStream (), "UTF-8") {String line; while (line = in. readLine ())! = Null) {result + = "\ n" + line ;}// obtain all the corresponding header fields Map <String, List <String> map = conn. getHeaderFields (); // traverses all corresponding header fields for (String key: map. keySet () {System. out. println (key + "--->" + map. get (key) ;}} catch (Exception e) {System. out. println ("an exception occurred when sending a Get request! "); E. printStackTrace ();} return result;} public static void main (String [] args) {// String s = GetPostTest. sendGet ("http: // localhost: 8080/NetWork_Code/index. jsp "," name = rapido & pwd = liying5201314 "); // System. out. println ("get:" + s); String s = GetPostTest. sendGet ("http: // localhost: 8080/NetWork_Code/data. json ", null); System. out. println ("get:" + s); // String s2 = GetPostTest. sendPost ("http: // localhost: 8080/NetWork_Code/index. jsp "," name = rapido & pwd = liying5201314 "); // System. out. println ("post:" + s2 );}}
Test an index. jsp page connection in a Web application.
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String name = request. getParameter ("name"); String pwd = request. getParameter ("pwd"); System. out. println ("name:" + name + ", pwd:" + pwd); %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> After the service is started, run the main method to return the following types of data:
File Size: 539null ---> [HTTP/1.1 200 OK] Date ---> [Sat, 30 May 2015 14:57:58 GMT] Content-Length ---> [539] Set-Cookie ---> [JSESSIONID = CD19ECBE035FDC1287CE22AD5CEF6DBA; Path =/NetWork_Code /; httpOnly] Content-Type ---> [text/html; charset = UTF-8] Server ---> [Apache-Coyote/1.1] get: <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">