0

Ok. My problem is that when I try to send HTTP PUT request to server through terminal in Java to the server "http://scratch.mit.edu/", I get the reply that the command does not exist. So I tried it directly from terminal. Still did not work. Is there ANY way at all that I do not have to install a program like curl or something so that I can do this? I am going to be sharing this program with a few other people, so I do not want to have to make them install a program. Through the Terminal, can I do this at all? Or, is there a WORKING java command? I've seen alot that people haven't tested it, and it doesn't work! Please help me!!!! D:

Here's my program (Nothing wrong with the program, only with finding a correct terminal command! D:)

    package com.mkyong.shell;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ExecuteShellComand {
    public static void main(String[] args) {
        ExecuteShellComand obj = new ExecuteShellComand();
        int studio = 497688;
        String command = "PUT /site-api/users/curators-in/" +studio +"/remove/?usernames=arinerron HTTP/1.1 \nHost: scratch.mit.edu \nAccept: */* \nAccept-Encoding: gzip,deflate,sdch \nAccept-Language: en-US,en;q=0.8 \nCookie: __qca=P0-1926269269-1400108554400; scratchcsrftoken=cQBcHRVvjG3LsQROJdCq1Ljmdi4bWnjB; scratchsessionsid=c36fe777199a51f3556f4ab97c62cc0a; __utma=133675020.292999539.1399158737.1406949614.1406996049.322; __utmb=133675020.85.9.1407000438801; __utmc=133675020; __utmz=133675020.1406740512.306.14.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided) \nDNT: 1 \nOrigin: http://scratch.mit.edu \nReferer: http://scratch.mit.edu/studios/" +studio +"/curators/ \nUser-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/34.0.1847.116 Chrome/34.0.1847.116 Safari/537.36 \nX-CSRFToken: cQBcHRVvjG3LsQROJdCq1Ljmdi4bWnjB \nX-Requested-With: XMLHttpRequest \nHTTP/1.1 200 OK \nAccept-Ranges: bytes \nAccess-Control-Allow-Credentials: true \nAccess-Control-Allow-Headers: Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control \nAccess-Control-Allow-Methods: OPTIONS, GET, POST \nAccess-Control-Allow-Origin: * \nAge: 0 \nConnection: keep-alive \nContent-Encoding: gzip \nContent-Language: en \nContent-Length: 287 \nContent-Type: text/html; charset=utf-8 \nDate: Sat, 02 Aug 2014 17:33:19 GMT \nServer: Scratch Web Server \nVary: Accept-Encoding, Accept-Language, Cookie \nVia: 1.1 varnish \nX-Cache: MISS \nX-Cache-Hits: 0 \nX-Varnish: 1986766295";
        String output = obj.executeCommand(command);
        System.out.println(output); 
    }

    private String executeCommand(String command) {
        StringBuffer output = new StringBuffer();
        Process p;
        try {
            p = Runtime.getRuntime().exec(command);
            p.waitFor();
            BufferedReader reader = 
                            new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = "";           
            while ((line = reader.readLine())!= null) {
                output.append(line + "\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return output.toString();

    }

}

Edit Sept 2017: Haha, this is a really dumb question. This was around the time when I was learning Java, so I was incapable of fixing it. I was 12 years old back then. Now, I would've used Apache httpcomponents for this-- HttpUrlConnection is pretty bad IMO, and sending http requests via terminal from Java is a horrendous thing to do.

Aaron Esau
  • 1,083
  • 3
  • 15
  • 31

1 Answers1

2

Is there ANY way at all that I do not have to install a program like curl or something so that I can do this?

There isn't a way.

Neither the UNIX / Linux or Windows command shells have built-in support for performing HTTP requests. If you are going to do this via the shell, you need to have curl or wget or some equivalent program installed.

But a (generally1) better way to make HTTP requests in Java is to either use HttpURLConnection, or the Apache HttpComponents library. They both work, if you use them properly.


1 - An external command such as curl may be more efficient if you are transferring large files to or from the local file system. However, that is a special case.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216