Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

but I don't want to read from the URL!  RSS feed

Josh Whiton
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written a very simple app to learn about java networking but it's not behaving as expected. The client program should take the string "hello", and open a URLConnection to my cgi script (perl). It should write to connection, the perl script should read the text, include a timestamp and write to a file. However, the perl script only completes when my Java program invokes "conection.getInputStream();" Even if Perl script has nothing to write to the URLConnection. I only want my Java prog to write the word "hello" to the connection. I do not want to read from it. That's it! Here's the important parts of the code.
String stringToSend = URLEncoder.encode("Hello","UTF-8");
URL url = new URL("http://myserver/cgi-bin/");
URLConnection connection = url.openConnection();

// set the connection so it can be written to

PrintWriter out = new PrintWriter(
out.print(stringToSend); //Writes to output stream.

//If i take this out the perl script never writes to the file!
<The Perl Script>
# Reads a string from an incoming URL connection and writes it and a
# timestamp to a file.
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
#Get the current system on the server
$date = `date`;
#Remove the new line character returned by date command
#Writes to a file
$filename = "textfile.txt";
open (FH,">>textfile.txt") || die "Error\n";
printf(FH "%s%s\n",$buffer,$date);
close FH;
exit 0;
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don' know the http-specifications, but I'm an experienced browser-user.
If I call a url in the browser, I expect some response - at least a 'page not found' or 'connection refused' response.
I guess if you 'misuse' the http-protocol, you should not complain about it
If I use the ticket-automat of the subway, I cannot make a contribution. I can put in some money, but I have to choose a ticket, or my money is refused. If I don't want a ticket, I still have to buy one, but I can throw it away
You may implement your own internet-protocol, using high (unreserved) port-numbers, but then you will have to implement the corresponding program on the server (which calls the perl...).
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!