5

Sending data from HttpURLConnection

view full story
linux-howto

http://www.daniweb.com – Hi, I have little experience with writing to Http socket. I am trying to send data to another computer through Http protocol, just like having a chat between two application but instead of using Socket connection I want to use Http connection. The server writes to http connection data and the client receives it: text, files, etc... is this possible? I tried to send text and integers to an URL and then read from that URL. I write something to the URL but when I read from it, it returns to sourcecode of that page :(. Is it possible to use Http connection instead of socket connection for an application such as socket.. to transfer files and messages?   try {             URL url = new URL("http://abcdefg.com");             try {                 HttpURLConnection conn = (HttpURLConnection) url.openConnection();                 conn.setDoOutput(true);                 DataOutputStream d = new DataOutputStream(conn.getOutputStream());                 String data = "123";                 data = URLEncoder.encode(data, "UTF-8");                 d.writeUTF(data);                 d.flush();                 d.close();             } catch (IOException ex) {                 Logger.getLogger(chatServer.class.getName()).log(Level.SEVERE, null, ex);             }         } catch (MalformedURLException ex) {             Logger.getLogger(chatServer.class.getName()).log(Level.SEVERE, null, ex);         } //////////////////////// try {             URL url = new URL("http://abcdefg.com");             try {                 HttpURLConnection conn = (HttpURLConnection) url.openConnection();                 conn.setDoInput(true);                 DataInputStream input = new DataInputStream(conn.getInputStream());                 System.out.println("numar = "+input.readUTF());                 input.close();             } catch (IOException ex) {                 Logger.getLogger(chatServer.class.getName()).log(Level.SEVERE, null, ex);             }         } catch (MalformedURLException ex) {             Logger.getLogger(chatServer.class.getName()).log(Level.SEVERE, null, ex);         } Thanks for reading. (General)