File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes Writing to a URLConnection object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Writing to a URLConnection object" Watch "Writing to a URLConnection object" New topic
Author

Writing to a URLConnection object

Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Jst getting my feet wet with java.net, either I am doing something wrong, or I misunderstand the concept of writing to a URLConnection.

I have constructed a little test program based on info at http://java.sun.com/docs/books/tutorial/networking/urls/readingWriting.html

In my environment, I have a Linux box running Apache WebServer. I have tested Apache and it is working fine.

I have placed an empty file test2.htm in the root directory of the webserver. The file has write access for everyone.

I am expecting that the String "Hello World" will get written to the file, but nothing is happening. The following code executes without error.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42273
    
  64
A static HTML page can not be written to using HTTP. If you want to alter a server-side file, you either need an active server component that does that (like a servlet), or you need to somehow obtain file access to it, so that you can use the java.io classes like File and FileOutputStream.


Ping & DNS - my free Android networking tools app
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Yes, you misunderstand what writing to a URL means.

When you make a URLConnection to an HTTP site, your open an HttpURLConnection and you get the OutputStream to it you are getting the OutputStream to the HTTPRequest being made to the server. The HTTPRequest isn't used to copy bytes to the page being requested, it is used to write request headers and form parameters (or data to be uploaded).


Steve
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Steve Luke wrote:Yes, you misunderstand what writing to a URL means.

When you make a URLConnection to an HTTP site, your open an HttpURLConnection and you get the OutputStream to it you are getting the OutputStream to the HTTPRequest being made to the server. The HTTPRequest isn't used to copy bytes to the page being requested, it is used to write request headers and form parameters (or data to be uploaded).


Thanks Ulf and Steve. That helps. My ultimate goals is to use the java.net classes to provide data from the client side to a php script, and then on to mySQL, as per the following thread http://www.coderanch.com/t/460335/Applets/java/Java-applet-communicating-with-internet, And I'm really not too sure how close I am to that goal, just working on the basics.

It seems that the remark of Steve's that I have bolded points in the right direction.

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

In that case, you are doing things correctly on the client side, you just are expecting incorrect results on the server side.

If I were using a JEE app for this, my goal would be to write a Servlet that maps to the /test2.htm URL. It would read the InputStream coming in from the client, then write a response in kind. I am guessing you should do the same with PHP, though I don't know enough about PHP to say how. Basically, read the Stream, form a response and send it. You might be better off using a Post response, not sure if that matters or not.

Then on the client side read the response from the PHP scripts and display it.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Steve Luke wrote:In that case, you are doing things correctly on the client side, you just are expecting incorrect results on the server side.

If I were using a JEE app for this, my goal would be to write a Servlet that maps to the /test2.htm URL. It would read the InputStream coming in from the client, then write a response in kind. I am guessing you should do the same with PHP, though I don't know enough about PHP to say how. Basically, read the Stream, form a response and send it. You might be better off using a Post response, not sure if that matters or not.

Then on the client side read the response from the PHP scripts and display it.


Ok, in my own environment that is probably doable, but on the internet I am using a free webhosting service, http://www.zymic.com and as far as I can see that service doesn't include the more advanced server side technology required for a professional solution.

So in that environment java ---> php ---> mySQL is my only option, unless you think otherwise?

thanks.

edit: I alrady have the reverse in place, in which html forms tell php what to extract from mySQL and pass on to java via applet param tags.

it is not intnded to be a true client server bi-directonal thing. the act of extracting from mySQL and writing to mySQL are completely separated.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Forgive my ignorance, but PHP is server-side technology right?

If so, and if it has the ability to read a requests input stream, then you can write to the OutputStream in Java, and read from it in PHP. It isn't Java --> PHP --> MySQL it is Java --> HTTP --> PHP --> MySQL.

Or, if PHP can't read directly from the stream then you can format your data as a form submission. Using a GET request you would make a URL which includes the parameter data:
http://192.168.1.103/test2.htm?value1=Whatever&value2=Something%20Else

If the data is more complex then you want to format the request as a POST request, which means properly formatting the data you send through the HttpURLConnection's OutputStream. It means setting the proper headers and request mode, so a bit of a search will help you out.

Or use the HttpClient from Apache. Makes things easier.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Steve Luke wrote:Forgive my ignorance, but PHP is server-side technology right?

If so, and if it has the ability to read a requests input stream, then you can write to the OutputStream in Java, and read from it in PHP. It isn't Java --> PHP --> MySQL it is Java --> HTTP --> PHP --> MySQL.

Or, if PHP can't read directly from the stream then you can format your data as a form submission. Using a GET request you would make a URL which includes the parameter data:
http://192.168.1.103/test2.htm?value1=Whatever&value2=Something%20Else

If the data is more complex then you want to format the request as a POST request, which means properly formatting the data you send through the HttpURLConnection's OutputStream. It means setting the proper headers and request mode, so a bit of a search will help you out.

Or use the HttpClient from Apache. Makes things easier.


yes, php is Server side script, it can be embedded in html, and parsed by an Apache Server that also has php. So my take on your post is that it is indeed doable the way I am headed.

I'm aware of HTTPClient from Apache, but know little about it.. I may do that for learning purposes, but it is not clear to me whether that is suitable for general purpose. Is it something that is transparent to any end user on the net who has JVM?

p.s. It isn't Java --> PHP --> MySQL it is Java --> HTTP --> PHP --> MySQL.

duly noted, I agree with this statement, now that you meantion it.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Yeah, using HTTPClient is transparent to the user as long as you package the HttpClient JAR along with your Applet. It will make the download a little bigger but it is well worth it in my opinion - especially if you end up using any other communication between the Applet and Server later on.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
cool. Well, I will look at the Apache stuff, but I'd like to see the Java-->HTTP-->PHP angle through to a conclusion first. If for no other reason than the learning experience.

Anyways, This is all pretty new. as I dig into this, I can see it's not going to be easy piecing it all together. One thing seems clear though, it doesn't seem to have a lot to do with socket programming. So I shouldn't have to concern myself with all that at this point.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42273
    
  64
Some of the examples in the "URL" section might be helpful, especially e135: http://www.exampledepot.com/egs/java.net/pkg.html
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Thanks Ulf, yes example 135 seems succinct enough, but...

This whole effort kind of underlines one of my biggest problems with learning java, and I imagine the same holds true for others.

So much knowledge/info can be contained in a single sentence, and it is not so easy to connect the dots. And by that I mean equating my description to their description. My requirements are expressed in terms of of "sending text to a variable in a php script", and someone else says do a POST request, and you find an example that says "this is how you do a post" and the example shows a URL to a cgi dirctory and UTF encode yadah yadah, and the connection between my requirments and the example is not clear. So you go on a google adventure and start reading about the relevance of UTF encode, and HTTP headers, and CGI scripts and .... You get the point. Such is the joy of learning java I guess. An alternative might be a fullblown course in network programming.

Anyways, I'm sorry to rant. I suppose a shorter description of my issue is "It is often not at all easy to abstract one's own requirements into a form that make the search for relevant knowledge easier.

regards.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Ulf Dittmer wrote:Some of the examples in the "URL" section might be helpful, especially e135: http://www.exampledepot.com/egs/java.net/pkg.html


Thanks Ulf. I finally got around to masking a serious effort to understand this example, and I am part way there.

I have constructed the following prototype in my own environment, which includes apache and php on the server and java on the workstation. The readLine() method in my code reads the output of the php script, in other words the act of reading from the URLConnection is akin to telling the webserver "send me the page specified by the URL object".



now, In the example you mention, you can see how they set up an output stream to the URLConnection and write to it. No problem there. Where it starts to get fuzzy is as follows. I hope you are familiar with php.

1. When an HTML form is used to POST to php, I understand that the data entered into the form, along with the names of the form fields, is assembled into a string of some kind and passed to the webserver along with the actual URL that points to the script.

2. I expect that the act of opening an outputStream to the URLConnection, then writing to that stream as is done in the example you suggested, is the technique that is used by java to duplicate the sending of the post Data in my point 1.

3. I expect the next step by java would then be to open an inputstream to read whatever response might by echoed by the php script.

4. Somewhere between steps 2 and 3 I figure the php server intercepts the script along with the data posted by java in the write operation and processes it according to the commands in the script.

ok here's the problem. I can't figure out how to assemble the data string that will be written to the URLCOnnection. I figure it will include the names of the php variables in the script and the actual data associated with them, and maybe also some http keywords. Internet examples I have been dug up, such as the one you provided, are good to a point, but haven't told me the actual form the write string should take.

Any help or direction or weblinks people can provide on this point is much appreciated.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42273
    
  64
The "write string" is assemble din the first statement that starts with "String data =". In that example two parameters are sent named "key1" and "key2", and their values are "value1" and "value2". You'd substitute your own variables and values for those, and add any others you may have.

You don't have to do anything HTTP-specific - URLConnection handles that (unless you want to accept cookies, that is - in which case I'd use the Apache Commons HttpClient library).
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
OK Ulf, I got it to work. Your help opens up a lot of doors for me to press ahead with my project. Thanks a lot.

here is my java code



and my php script



and my output




Chris Giles
Greenhorn

Joined: Mar 03, 2010
Posts: 1
This post helped me today!

I was trying to figure out the same thing. Sending data from an activity on an android to a web side php based page turned out to be easy, but replying back to the activity on the android was a little hairy. Issue solved!
zxcv bnnm
Greenhorn

Joined: Nov 09, 2013
Posts: 1
Hello i have the site where is login and password, im using your code and it working good im getting html of the site after login but here my question i want to set the other value in the loged site but i dont know how i tried add after out.flush();


but it try to send to first site where need to set login and password and i getting info with wrong password or login.

Can someone help me?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42273
    
  64
For programmatic web access I recommend the HtmlUnit library; it makes it much easier to retrieve and submit forms.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing to a URLConnection object