daily pastebin goal
45%
SHARE
TWEET

RequestAPI servlet

a guest Dec 16th, 2018 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package fys;
  2.  
  3. import javax.servlet.ServletException;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8.  
  9. import org.apache.http.HttpEntity;
  10. import org.apache.http.HttpResponse;
  11. import org.apache.http.client.HttpClient;
  12. import org.apache.http.client.methods.HttpPost;
  13. import org.apache.http.entity.ContentType;
  14. import org.apache.http.entity.StringEntity;
  15. import org.apache.http.impl.client.HttpClientBuilder;
  16.  
  17. import java.io.BufferedReader;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.InputStreamReader;
  21. import java.io.PrintWriter;
  22. import java.util.stream.Collectors;
  23.  
  24. @WebServlet(name = "Servlet", urlPatterns = {"/index.html"}) //little bit of a hack. no need for extra url because browsers always look for index.html
  25. public class Servlet extends HttpServlet {
  26.    
  27.    
  28.     protected static String RequestAPI(String payload,String function) throws UnsupportedOperationException, IOException {
  29.         String result = null;
  30.         System.out.println(payload);
  31.         StringEntity entity = new StringEntity(payload,
  32.                 ContentType.APPLICATION_FORM_URLENCODED);
  33.         System.out.println(payload);
  34.         HttpClient httpClient = HttpClientBuilder.create().build();
  35.         String choice = function;
  36.         String url = String.format("http://fys.securidoc.nl:11111/%s", choice);
  37.         HttpPost dbrequest = new HttpPost(url);
  38.         dbrequest.setEntity(entity);
  39.  
  40.         HttpResponse response = httpClient.execute(dbrequest);
  41.         System.out.println(response.getStatusLine().getStatusCode());
  42.  
  43.         HttpEntity entityresponse = response.getEntity();
  44.         if (entityresponse != null) {
  45.             InputStream instream = entityresponse.getContent();
  46.             result = convertStreamToString(instream);
  47.         }
  48.         return result;
  49.     }
  50.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  51.         response.setContentType("text/html;charset=UTF-8");
  52.         PrintWriter out = response.getWriter();
  53.         try {
  54.             //out.println("<!DOCTYPE html>");
  55.             //out.println("<html>");
  56.             //out.println("<head>");
  57.             //out.println("<link href=\"style/style.css\" type=\"text/css\" rel=\"stylesheet\">");
  58.             //out.println("<title>Servlet myServlet</title>");
  59.             //out.println("</head>");
  60.             //out.println("<body>");
  61.             //out.println("<h1>Servlet myServlet at " + request.getContextPath() + "</h1>");
  62.             //out.println("</body>");
  63.             //out.println("</html>");
  64.  
  65.  
  66.             //own stuff
  67.             //  DO NOT FORGET TO MOVE RESOURCE(CSS, IMG) FOLDERS TO WEB DIRECTORY
  68.             out.println("<!DOCTYPE html>");
  69.             out.println("<html lang=\"nl\">");
  70.             out.println("<head>");
  71.             out.println("<title>WiFi in het vliegtuig | Corendon</title>");
  72.             out.println("<meta charset=\"UTF-8\">");
  73.             out.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">");
  74.             out.println("<link href=\"style/style.css\" type=\"text/css\" rel=\"stylesheet\">");
  75.             out.println("<script src=\"scripts/myscripts.js\"></script>");
  76.             out.println("</head>");
  77.             out.println("");
  78.             out.println("<body id=\"loginPage\" onload=\"startTime()\">");
  79.             out.println("<header>");
  80.             out.println("<figure>");
  81.             out.println("<img src=\"img/logo.jpg\" alt=\"Corendon_Logo\">");
  82.             out.println("</figure>");
  83.             out.println("<figure class=\"gradient\">");
  84.             out.println("</figure>");
  85.             out.println("</header>");
  86.             out.println("<article class=\"login\">");
  87.             out.println("<h1>Gratis internet in het vliegtuig</h1>");
  88.             out.println("<p>Log in met uw gegevens:</p>");
  89.             out.println("<form action=\"#\" method=\"post\">");
  90.             out.println("<input type=\"text\" name=\"achternaam\" placeholder=\"Achternaam..\" required autofocus>");
  91.             out.println("<input type=\"text\" name=\"boekingsnummer\" placeholder=\"Boekingsnummer..\" required>");
  92.             out.println("<label>");
  93.             out.println("<input type=\"checkbox\" name=\"check\" value=\"check\" required>");
  94.             out.println("Ik ga akkoord met de <a href=\"doc/Voorwaarden.pdf\" target=\"_blank\">voorwaarden</a> en het <a href=\"doc/Privacybeleid.pdf\" target=\"_blank\">privacybeleid</a>");
  95.             out.println("</label>");
  96.             out.println("<input type=\"submit\" value=\"MAAK VERBINDING\">");
  97.             out.println("</form>");
  98.             out.println("</article>");
  99.             out.println("<article class=\"info\">");
  100.             out.println("<h1 id=\"clock\"></h1>");
  101.             out.println("</article>");
  102.             out.println("</body>");
  103.             out.println("</html>");
  104.         } finally {
  105.             out.close();
  106.         }
  107.     }
  108.  
  109.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  110.         PrintWriter out = response.getWriter();
  111.         out.println("<script>");
  112.         String userdata = request.getReader().lines().collect(Collectors.joining());
  113.         out.println(String.format("console.log(\"%s\")", userdata));
  114.         out.println("</script>");
  115.         if (userdata.contains("admin")) {
  116.             out.println("<meta http-equiv=\"refresh\" content=\"0; URL='http://nu.nl'\" />"); //redirection example
  117.         }
  118.  
  119.     }
  120.    
  121.     private static String convertStreamToString(InputStream is) {
  122.          
  123.         BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  124.         StringBuilder sb = new StringBuilder();
  125.  
  126.         String line = null;
  127.         try {
  128.             while ((line = reader.readLine()) != null) {
  129.                 sb.append(line + "\n");
  130.             }
  131.         } catch (IOException e) {
  132.             e.printStackTrace();
  133.         } finally {
  134.             try {
  135.                 is.close();
  136.             } catch (IOException e) {
  137.                 e.printStackTrace();
  138.             }
  139.         }
  140.         return sb.toString();
  141.     }
  142.    
  143.    
  144. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top