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?
-
2The 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 Answers
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.

- 11
- 4
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.

- 126,289
- 21
- 250
- 231
-
thanks but if u give me some complete example then it will be easier for me. I need some more clarification. – user556761 Feb 16 '11 at 04:30
-
Can any one give the complete code of sending an HTTP request in Java to a web server written in C. – user556761 Feb 16 '11 at 04:39
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.

- 15,454
- 6
- 47
- 56
-
thanks but i also need a web interface of our server which is written in c – user556761 Feb 16 '11 at 04:59
-
Meh, I think thats asking a bit much. What code have you tried yourself? – Mr. Shickadance Feb 16 '11 at 05:02
-
I am trying for a web interface to our server which is a programme written in c . – user556761 Feb 16 '11 at 05:32
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!

- 61
- 3