OrangoMango

HTTP POST

Feb 21st, 2022 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.23 KB | None | 0 0
  1. package com.orangomango.httppostrequest;
  2.  
  3. import java.io.*;
  4. import java.net.HttpURLConnection;
  5. import java.net.URL;
  6. import java.net.URLConnection;
  7. import java.nio.file.Files;
  8.  
  9. public class Main {
  10.  
  11.     public static String username;
  12.     public static String password;
  13.  
  14.     public static void checkParam(String param) {
  15.         try {
  16.             URL url = new URL("http://orangomango.000webhostapp.com/account/user_data.php?" + param);
  17.             HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  18.             //conn.setRequestMethod("GET");
  19.  
  20.             try {
  21.                 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  22.                 reader.lines().forEach(line -> System.out.println(line));
  23.                 reader.close();
  24.             } catch (IOException ioe) {
  25.             }
  26.         } catch (Exception e) {
  27.         }
  28.     }
  29.  
  30.     public static void uploadFile(File path, File binaryFile) {
  31.        
  32.         try {
  33.             String url = "http://orangomango.000webhostapp.com/upload.php";
  34.             String charset = "UTF-8";
  35.             File textFile = new File(path.getAbsolutePath());
  36.             String boundary = "84493"; //Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
  37.             String CRLF = "\n"; // Line separator required by multipart/form-data.
  38.  
  39.             URLConnection connection = new URL(url).openConnection();
  40.             connection.setDoOutput(true);
  41.             connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
  42.  
  43.             try (
  44.                 OutputStream output = connection.getOutputStream();
  45.                 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output, charset));
  46.             ) {
  47.                 // Send post data
  48.                 writer.write("--" + boundary + CRLF);
  49.                 writer.write("Content-Disposition: form-data; name=\"test\""+CRLF);
  50.                 writer.append(CRLF);
  51.                 writer.flush();
  52.                 writer.write("okiedokie"+CRLF);
  53.                 writer.flush();
  54.                
  55.                 // Send text file.
  56.                 writer.write("--" + boundary + CRLF);
  57.                 //writer.write(CRLF);
  58.                 writer.write("Content-Disposition: form-data; name=\"upFile\"; filename=\"" + textFile.getName() + "\"" + CRLF);
  59.                 //writer.write(CRLF);
  60.                 writer.write("Content-Type: text/plain; charset=" + charset + CRLF);
  61.                 //writer.write(CRLF); // Text file itself must be saved in this charset!
  62.                 writer.write(CRLF);
  63.                 writer.flush();
  64.                 Files.copy(textFile.toPath(), output);
  65.                 output.flush(); // Important before continuing with writer!
  66.                 writer.write(CRLF);
  67.                 writer.flush(); // CRLF is important! It indicates end of boundary
  68.  
  69.                 // Send binary file.
  70.                 writer.append("--" + boundary).append(CRLF);
  71.                 writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF);
  72.                 writer.append("Content-Type: "+URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF);
  73.                 writer.append("Content-Transfer-Encoding: binary").append(CRLF);
  74.                 writer.append(CRLF).flush();
  75.                 Files.copy(binaryFile.toPath(), output);
  76.                 output.flush(); // Important before continuing with writer!
  77.                 writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.
  78.  
  79.                            
  80.                 // End of multipart/form-data.
  81.                 writer.write("--" + boundary + "--");
  82.                 writer.write(CRLF);
  83.                 writer.flush();
  84.                
  85.                 // Output file
  86.                 try {
  87.                     BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  88.                     reader.lines().forEach(line -> System.out.println(line));
  89.                     reader.close();
  90.                 } catch (IOException ioe) {
  91.                 }
  92.                
  93.                 // Request is lazily fired whenever you need to obtain information about response.
  94.                 int responseCode = ((HttpURLConnection) connection).getResponseCode();
  95.                 System.out.println(responseCode); // Should be 200
  96.             }  
  97.         } catch (Exception e){}
  98.     }
  99.  
  100.     public static void main(String[] args) {
  101.         try {
  102.             URL url = new URL("http://orangomango.000webhostapp.com/account/account.php");
  103.  
  104.             try {
  105.                 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  106.                 System.out.print("Username: ");
  107.                 username = reader.readLine();
  108.                 System.out.print("Password: ");
  109.                 password = reader.readLine();
  110.                 reader.close();
  111.             } catch (IOException ioe){}        
  112.            
  113.             String postData = String.format("username=%s&password=%s", username, password);
  114.             System.out.println("Username? ");
  115.             checkParam("method=user&param="+username);
  116.             System.out.println("Password? ");
  117.             checkParam("method=password&param="+username+"+"+password);
  118.  
  119.  
  120.             // Upload files
  121.                    
  122.             File f = new File("C:\\Users\\Khloud\\Documents\\NetBeansProjects\\httpPostRequest\\src\\main\\java\\com\\orangomango\\httppostrequest\\test.wld");
  123.             System.out.println("F exists? "+f.exists());
  124.             uploadFile(f, new File("C:\\Users\\Khloud\\Documents\\NetBeansProjects\\httpPostRequest\\src\\main\\java\\com\\orangomango\\httppostrequest\\test.jpg"));
  125.  
  126.             HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  127.             conn.setRequestMethod("POST");
  128.             conn.setDoOutput(true);
  129.  
  130.             try (BufferedWriter dos = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()))) {
  131.                 dos.write(postData);
  132.             }
  133.  
  134.             int responsecode = conn.getResponseCode();
  135.             System.out.println(responsecode);
  136.         } catch (Exception e) {
  137.         }
  138.     }
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment