Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 5th, 2012  |  syntax: None  |  size: 1.10 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How do I get my Java code to display results after connection?
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import java.io.OutputStreamWriter;
  5. import java.net.URL;
  6. import java.net.URLConnection;
  7. import java.net.URLEncoder;
  8.  
  9. public class Connect {
  10.  
  11.  
  12.     public void POSTDATA() {
  13.     }
  14.     public static void main(String[] args) {
  15.         try {
  16.         // Construct data
  17.         String data = URLEncoder.encode("ipaddress", "UTF-8") + "=" + URLEncoder.encode("98.36.2.53", "UTF-8");
  18.  
  19.  
  20.         // Send data
  21.         URL url = new URL("http://localhost/myfiles/WorkingVersion.php");
  22.         URLConnection conn = url.openConnection();
  23.         conn.setDoOutput(true);
  24.         OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
  25.         wr.write(data);
  26.         wr.flush();
  27.  
  28.         // Get the response
  29.         BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  30.         String line;
  31.         while ((line = rd.readLine()) != null) {
  32.             System.out.println(line);
  33.      }
  34.         wr.close();
  35.         rd.close();
  36.     } catch (Exception e) {
  37.     }
  38.     }
  39.  
  40. }