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 {/** * sends a GET method request to the specified URL */public static string sendget (String url,string param) {string result = ""; String urlname = Url+ "?" +param;try {URL realurl = new URL (urlname);//Open JOIN URLConnection conn = Realurl.openconnection ();// Set general request Properties Conn.setrequestproperty ("Accept", "*/*"), Conn.setrequestproperty ("Connection", "keep-alive"); Conn.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.1; SV1);//Establish the actual connection conn.connect (); SYSTEM.OUT.PRINTLN ("File Size:" +conn.getcontentlength ());//Get all corresponding header fields map<string, list<string>> map = Conn.getheaderfields ();//traverse all corresponding header fields for (String Key:map.keySet ()) {System.out.println (key+ "--->" +map.get (Key));} Defines the BufferedReader input stream to read the URL of 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 ("Send GET request exception!") "); E.printstacktrace ();} return result;} public static string Sendpost (String url,string param) {string result = ""; try {url realurl = new URL (URL);//Open Join Urlconnec tion conn = realurl.openconnection ();//Set Generic Request Properties Conn.setrequestproperty ("Accept", "*/*"); Conn.setrequestproperty (" Connection "," keep-alive "); Conn.setrequestproperty (" User-agent "," mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1);//The Send POST request must have the following two lines Conn.setdooutput (true) Conn.setdoinput (true);//Get the URLConnection object corresponding to the output stream try (printwriter out = new PrintWriter (Conn.getoutputstream ())) {out.print (param);//Send request parameter Out.flush ();//Flush output stream buffer}//definition BufferedReader the input stream to read the URL of the corresponding try (bufferedreader in = new BufferedReader (New InputStreamReader (Conn.getinputstream (), " Utf-8 ")) {String Line;while (line = In.readline ())!=null) {result + =" \ n "+line;}} Get all the corresponding header fields map<string, List<string>> map = Conn.getheaderfields ();//traverse all corresponding header fields for (String Key:map.keySet ()) {System.out.println (key+ "--->" +map.get (key));}} catch (Exception e) {System.out.println ("Send GET request exception!") "); 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);}}
Testing a index.jsp page join 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 starting the service, execute the main method to return the following type of data
File size: 539null--->[http/1.1 ok]date--->[sat, 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" >
Send a GET request to a Web site, post a request, and get data samples from the Web site