0

I have to send an HTTP request to our C programme which is running on a Linux machine. How can I send an HTTP request in Java to our server which is in C and running on a Linux machine?

John Rasch
  • 57,880
  • 19
  • 101
  • 136
user556761
  • 201
  • 2
  • 5
  • 9
  • 2
    The language in which your server is written and the OS it runs on are both irrelevant to this question. – Stephen C Feb 16 '11 at 05:06
  • possible duplicate of [How to send HTTP request in java?](http://stackoverflow.com/questions/1359689/how-to-send-http-request-in-java) – Stephen C Feb 16 '11 at 05:08

4 Answers4

1
 public void sendPostRequest() {

    //Build parameter string
    String data = "width=50&height=100";
    try {

        // Send the request
        URL url = new URL("http://www.somesite.com");
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

        //write parameters
        writer.write(data);
        writer.flush();

        // Get the response
        StringBuffer answer = new StringBuffer();
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            answer.append(line);
        }
        writer.close();
        reader.close();

        //Output the response
        System.out.println(answer.toString());

    } catch (MalformedURLException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

The above example is for sending a POST request using a URL.

Rahul
  • 11
  • 4
0

If you're asking how to send an HTTP request in Java to a web server written in C, you can use the URLConnection class.

icktoofay
  • 117,602
  • 18
  • 233
  • 223
0
try {
// Construct data
String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");

// Send data
URL url = new URL("http://hostname:80/cgi");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();

// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
    // Process line...
}
wr.close();
rd.close(); } catch (Exception e) { }

The above example is for sending a POST request using a URL. Also take a look at Sun Tutorial on reading/Writing from/to a URLConnection. The other option is to use Apache HTTPComponents which has examples for the HttpCore and HttpClient module. If you are looking into implementing the web Server, you will have to handle the Http request yourselves which involves a thread pool, parsing the requests, generating HTML, security, multiple sessions, etc or follow the easy route by using off-the-shelf web server like Apache and seeing which all high-level languages like Perl, Ruby can be used for developing the web application. For implementing your own Http server, please take a look at Micro-Httpd or tinyHttpd You may also want to look at Adding Web Interface -C++ application which has a sample code.

Piyush Mattoo
  • 14,524
  • 6
  • 49
  • 58
0

From the way your question is worded.. I think you need to know some basic stuff before you can start. Try try googling for a simple guide to how web servers work.

Once you have the basic idea, there are a couple of options for a C programmer:

1) You want your C program to be running continuously, waiting for a request from your Java.

In this case, you will have to code your C program to open a Socket and Listen for connections. See http://www.linuxhowtos.org/C_C++/socket.htm for example.

OR

2) You have a web Server on your server which will run your C program each time a particular request is made? In this case, you will have to code your C as a CGI program. See http://www.cs.tut.fi/~jkorpela/forms/cgic.html for example.

Hint: (2) is much easier!