http://hbiao68.iteye.com/blog/1973914
Write a servlet for test requests
Java code
- Import java.io.IOException;
- Import Java.io.PrintWriter;
- Import javax.servlet.ServletException;
- Import Javax.servlet.http.HttpServlet;
- Import Javax.servlet.http.HttpServletRequest;
- Import Javax.servlet.http.HttpServletResponse;
- Public class ABC extends HttpServlet {
- protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
- System.out.println ("doget");
- This.dopost (request, response);
- }
- protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
- System.out.println ("DoPost");
- System.out.println ("AAA:" +request.getparameter ("aaa"));
- System.out.println ("param1:" +request.getparameter ("param1"));
- System.out.println (Request.getheader ("AVC"));
- PrintWriter pw = Response.getwriter ();
- Pw.write (Request.getparameter ("param1"));
- Pw.flush ();
- Pw.close ();
- }
- }
Web. XML configuration file
XML code
- <servlet>
- <description></description>
- <display-name>abc</display-name>
- <servlet-name>abc</servlet-name>
- <servlet-class>com. ABC</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>abc</servlet-name>
- <url-pattern>/abc</url-pattern>
- </servlet-mapping>
Abstract out a tool request class
Java code
- Package http;
- Import Java.io.BufferedReader;
- Import Java.io.InputStreamReader;
- Import Java.io.OutputStreamWriter;
- Import java.net.HttpURLConnection;
- Import Java.net.URL;
- Import Java.util.Map;
- Import Java.util.Map.Entry;
- Public class Httputil {
- public static string http (string URL, map<string, string> params) {
- URL u = null;
- HttpURLConnection con = null;
- //Build request Parameters
- StringBuffer sb = new StringBuffer ();
- if (params! = null) {
- for (entry<string, string> e:params.entryset ()) {
- Sb.append (E.getkey ());
- Sb.append ("=");
- Sb.append (E.getvalue ());
- Sb.append ("&");
- }
- Sb.substring (0, Sb.length ()- 1);
- }
- System.out.println ("send_url:" + URL);
- System.out.println ("send_data:" + sb.tostring ());
- //Try to send a request
- try {
- u = new URL (URL);
- Con = (httpurlconnection) u.openconnection ();
- ////Post can only be uppercase, strictly limited, post will not recognize
- Con.setrequestmethod ("POST");
- Con.setdooutput (true);
- Con.setdoinput (true);
- Con.setusecaches (false);
- Con.setrequestproperty ("Content-type", "application/x-www-form-urlencoded");
- OutputStreamWriter OSW = new OutputStreamWriter (Con.getoutputstream (), "UTF-8");
- Osw.write (Sb.tostring ());
- Osw.flush ();
- Osw.close ();
- } catch (Exception e) {
- E.printstacktrace ();
- } finally {
- if (con! = null) {
- Con.disconnect ();
- }
- }
- //Read back content
- StringBuffer buffer = new StringBuffer ();
- try {
- //must have a return value, otherwise the request cannot be sent to the server side.
- BufferedReader br = new BufferedReader (new InputStreamReader (Con.getinputstream (), "UTF-8"));
- String temp;
- While (temp = br.readline ()) = null) {
- Buffer.append (temp);
- Buffer.append ("\ n");
- }
- } catch (Exception e) {
- E.printstacktrace ();
- }
- return buffer.tostring ();
- }
- }
Note: Be sure to get the return value, otherwise the request cannot be reached.
ReadLine () is a blocking method that waits for a response from the server or waits
Con.setdooutput (TRUE); This representation is to send a request to the URL address, without which the server will not respond.
Parameters are passed in a way similar to adding "? key=value&key1=value1" after the URL.
Test class
Java code
- @Test
- public void Test3 () {
- Map parames = new hashmap<string, string> ();
- Parames.put ("param1", "Param1_value");
- Parames.put ("param2", "Param2_value");
- Parames.put ("param3", "Param3_value");
- If there is an AAA parameter in the Address bar, the address bar is selected by default, and if not, select the parameter to add
- Parames.put ("AAA", "Aaa_value");
- Httputil.http ("http://localhost:8080/a/Abc?aaa=dddd", parames);
- }
Java sends HTTP requests (submits form form)