URL Connection


I have a class keyPressed

private void formKeyPressed(java.awt.event.KeyEvent evt) {  
switch (evt.getKeyChar ())
        case 'a':

            kiri1 = new kiri();

public void kiri()
             try {
                      URL url = new URL ("");
                      String protocol = url.getProtocol (); / / "http"
                      String host = url.getHost (); / / "Domain or ip"
                      String filename = url.getFile () / / '/ kanan.htm "
                      int port = url.getPort (); / / -1
                      if (url.getPort () == -1) port = 80; / / if no port, use the default HTTP port

                      / / Open a network socket connection to the specified host and port
                      / / And open PrintWriter and inputStream on connection

                      Socket socket = new Socket (host, port);
                      InputStream from_server = socket.getInputStream ();
                      PrintWriter to_server = new PrintWriter (new OutputStreamWriter (socket.getOutputStream ()));

                      / / Send the HTTP GET command to the Web server, specifying the file.

                      to_server.print ("GET" + filename + "\ r \ n \ r \ n");
                      to_server.flush (); / / Send it right now!

                      / / Now read the server's response, and write it to the printer

                      byte [] buffer = new byte [4096];
                      int bytes_read;
                      String text = ""; / / Un StringBuffer serait mieux!

                      while ((bytes_read = from_server.read (buffer))! = -1) {
                        System.out.write (buffer, 0, bytes_read);
                        text = text + new String (buffer, 0, bytes_read);
                     socket.close ();
                    catch (Exception e) {
                      System.err.println (e);
                      System.err.println ("Usage: java HttpClient");

when i press 'a' he will continue (loop) to call the class kiri();
how to call the class once I kiri (); //no loop

help me!!!!

HTTP/1.0 302 Found

HTTP/1.0 302 Found

HTTP/1.0 302 Found
Location: 302 Found

Location: 302 Found


HTTP/1.0 302 Found





admin's picture

I'm a little lost on how you

I'm a little lost on how you want it to behave.

When you press 'a' do you want it to continuously get and process a HTTP request?

The output wants you to be redirected - but doesn't say where...

akbar's picture

 I wanted just once to

I wanted just once to get and process the HTTP request?

admin's picture

What exactly is currently

What exactly is currently happening?

akbar's picture

  when I did not remove


when I did not remove the 'a' he continued to process http
so, what I want is just a one time my process, not continuously

I create program a button on / off to the motor by using http?
so the function press 'a' it is to turn the motor and release 'a' turn off the motor

If the program is constantly accessing the http, then the chain of the motor driver will be slow and could be off.

admin's picture

I believe I'm missing half

I believe I'm missing half the code, part of the behavior depends on the thing that is listening to the HTTP request...

Does ""  tell the web server to move the motor?  Is there another page/request which tells the web server to stop?  e.g.

Capturing the keyrelease is a different method.

If so you can do something like this... 


public void keyReleased(KeyEvent keyEvent) {
      kiri1 = new kiri("");