943,097 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2810
  • Java RSS
Mar 5th, 2010
0

Sending data from HttpURLConnection

Expand Post »
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?

Java Syntax (Toggle Plain Text)
  1. try {
  2. URL url = new URL("http://abcdefg.com");
  3. try {
  4. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  5. conn.setDoOutput(true);
  6. DataOutputStream d = new DataOutputStream(conn.getOutputStream());
  7.  
  8. String data = "123";
  9.  
  10. data = URLEncoder.encode(data, "UTF-8");
  11.  
  12. d.writeUTF(data);
  13. d.flush();
  14. d.close();
  15. } catch (IOException ex) {
  16. Logger.getLogger(chatServer.class.getName()).log(Level.SEVERE, null, ex);
  17. }
  18. } catch (MalformedURLException ex) {
  19. Logger.getLogger(chatServer.class.getName()).log(Level.SEVERE, null, ex);
  20. }
  21.  
  22.  
  23. ////////////////////////
  24.  
  25. try {
  26. URL url = new URL("http://abcdefg.com");
  27. try {
  28. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  29. conn.setDoInput(true);
  30. DataInputStream input = new DataInputStream(conn.getInputStream());
  31.  
  32.  
  33.  
  34. System.out.println("numar = "+input.readUTF());
  35. input.close();
  36. } catch (IOException ex) {
  37. Logger.getLogger(chatServer.class.getName()).log(Level.SEVERE, null, ex);
  38. }
  39. } catch (MalformedURLException ex) {
  40. Logger.getLogger(chatServer.class.getName()).log(Level.SEVERE, null, ex);
  41. }

Thanks for reading.
Similar Threads
Reputation Points: 11
Solved Threads: 7
Posting Whiz in Training
Clawsy is offline Offline
225 posts
since Feb 2008
Mar 5th, 2010
0
Re: Sending data from HttpURLConnection
uhm,clawsy we now at another big debate...

definitely i know so far we should use TCP FOR send data from one pc to another....

http protocol it depends server to client not in the case of peer to peer...

so i also think about how do get data through http for peer to peer...
Reputation Points: 19
Solved Threads: 45
Posting Whiz
musthafa.aj is offline Offline
312 posts
since Nov 2009
Mar 5th, 2010
0
Re: Sending data from HttpURLConnection
Yes... I'm just wondering if this if possible.... and if it is.. how can be done.
Reputation Points: 11
Solved Threads: 7
Posting Whiz in Training
Clawsy is offline Offline
225 posts
since Feb 2008
Mar 5th, 2010
0
Re: Sending data from HttpURLConnection
clawsy,i have one idea that is,

we should store the data in web and other end should read those data as update frequently...

like p1 and p2 interact with centralized web site from there they can exchange their data respectively using their session pass word...

can u understand what i come to say????????????

ok i now going to home...and come tomorrow with some ideas...
Last edited by musthafa.aj; Mar 5th, 2010 at 9:53 am.
Reputation Points: 19
Solved Threads: 45
Posting Whiz
musthafa.aj is offline Offline
312 posts
since Nov 2009
Mar 6th, 2010
0
Re: Sending data from HttpURLConnection
I understand but it's less performant. I need to write to the Http Socket or something like that. If you don't store it in the data buffer it's not performant....
Reputation Points: 11
Solved Threads: 7
Posting Whiz in Training
Clawsy is offline Offline
225 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: add counter in insertion sort program
Next Thread in Java Forum Timeline: pc is connected to internet ???





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC