Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.orangomango.httppostrequest;
- import java.io.*;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.net.URLConnection;
- import java.nio.file.Files;
- public class Main {
- public static String username;
- public static String password;
- public static void checkParam(String param) {
- try {
- URL url = new URL("http://orangomango.000webhostapp.com/account/user_data.php?" + param);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- //conn.setRequestMethod("GET");
- try {
- BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- reader.lines().forEach(line -> System.out.println(line));
- reader.close();
- } catch (IOException ioe) {
- }
- } catch (Exception e) {
- }
- }
- public static void uploadFile(File path, File binaryFile) {
- try {
- String url = "http://orangomango.000webhostapp.com/upload.php";
- String charset = "UTF-8";
- File textFile = new File(path.getAbsolutePath());
- String boundary = "84493"; //Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
- String CRLF = "\n"; // Line separator required by multipart/form-data.
- URLConnection connection = new URL(url).openConnection();
- connection.setDoOutput(true);
- connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
- try (
- OutputStream output = connection.getOutputStream();
- BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output, charset));
- ) {
- // Send post data
- writer.write("--" + boundary + CRLF);
- writer.write("Content-Disposition: form-data; name=\"test\""+CRLF);
- writer.append(CRLF);
- writer.flush();
- writer.write("okiedokie"+CRLF);
- writer.flush();
- // Send text file.
- writer.write("--" + boundary + CRLF);
- //writer.write(CRLF);
- writer.write("Content-Disposition: form-data; name=\"upFile\"; filename=\"" + textFile.getName() + "\"" + CRLF);
- //writer.write(CRLF);
- writer.write("Content-Type: text/plain; charset=" + charset + CRLF);
- //writer.write(CRLF); // Text file itself must be saved in this charset!
- writer.write(CRLF);
- writer.flush();
- Files.copy(textFile.toPath(), output);
- output.flush(); // Important before continuing with writer!
- writer.write(CRLF);
- writer.flush(); // CRLF is important! It indicates end of boundary
- // Send binary file.
- writer.append("--" + boundary).append(CRLF);
- writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF);
- writer.append("Content-Type: "+URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF);
- writer.append("Content-Transfer-Encoding: binary").append(CRLF);
- writer.append(CRLF).flush();
- Files.copy(binaryFile.toPath(), output);
- output.flush(); // Important before continuing with writer!
- writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.
- // End of multipart/form-data.
- writer.write("--" + boundary + "--");
- writer.write(CRLF);
- writer.flush();
- // Output file
- try {
- BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- reader.lines().forEach(line -> System.out.println(line));
- reader.close();
- } catch (IOException ioe) {
- }
- // Request is lazily fired whenever you need to obtain information about response.
- int responseCode = ((HttpURLConnection) connection).getResponseCode();
- System.out.println(responseCode); // Should be 200
- }
- } catch (Exception e){}
- }
- public static void main(String[] args) {
- try {
- URL url = new URL("http://orangomango.000webhostapp.com/account/account.php");
- try {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- System.out.print("Username: ");
- username = reader.readLine();
- System.out.print("Password: ");
- password = reader.readLine();
- reader.close();
- } catch (IOException ioe){}
- String postData = String.format("username=%s&password=%s", username, password);
- System.out.println("Username? ");
- checkParam("method=user¶m="+username);
- System.out.println("Password? ");
- checkParam("method=password¶m="+username+"+"+password);
- // Upload files
- File f = new File("C:\\Users\\Khloud\\Documents\\NetBeansProjects\\httpPostRequest\\src\\main\\java\\com\\orangomango\\httppostrequest\\test.wld");
- System.out.println("F exists? "+f.exists());
- uploadFile(f, new File("C:\\Users\\Khloud\\Documents\\NetBeansProjects\\httpPostRequest\\src\\main\\java\\com\\orangomango\\httppostrequest\\test.jpg"));
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("POST");
- conn.setDoOutput(true);
- try (BufferedWriter dos = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()))) {
- dos.write(postData);
- }
- int responsecode = conn.getResponseCode();
- System.out.println(responsecode);
- } catch (Exception e) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment