URLConnection - Alternative?

Dear Friends,

the URLConnection class is buffering at the beginning and it is unusable for a progressbar-feature. (directly 100 % - but is still transfering)

Is there a class which is similar to URLConnection but without buffer or cache?

Thank you very much!

MfG

Inno

[318 byte] By [Innocentusa] at [2007-11-27 5:01:20]
# 1
Yes: java.net.Socket
bsampieria at 2007-7-12 10:18:39 > top of Java-index,Java Essentials,Java Programming...
# 2
You sure this is not a swing GUI Thread issue? - remember that the JProgressBar needs to be updated outside the GUI Thread, otherwise it will wait until the work is done and update it all at once. If the work goes quickly, it might look like it's going strait to 100%
abillconsla at 2007-7-12 10:18:39 > top of Java-index,Java Essentials,Java Programming...
# 3
> You sure this is not a swing GUI Thread issue? I suspect it's [url= http://forum.java.sun.com/thread.jspa?threadID=5159807]this issue[/url].~
yawmarka at 2007-7-12 10:18:39 > top of Java-index,Java Essentials,Java Programming...
# 4

Well, the Socket Class could be a solution.

1. But how can I open a channel to a HTTP-Server?

2. And how can I send the HTTP headers?

3. The best would be to wrap the stuff into a new Class called "URLConnectionSocket" or like that. So I don't need to restructure everything but to replace some portions of my uploader-class-code...

Btw.: (The thread which was posted): Deactivation of the Caching / Buffering features of the URLConnection didn't solve the problem in any way.

Because the caching into some ByteArray is hard-coded the URLConnection class is not usable for progressBars.

And - no - it is not a GUI-Problem because I output the results on the console. And the console is thread-save (yea).

Thank you very much

With best regards

MfG

Inno

Message was edited by:

Innocentus

Message was edited by:

Innocentus

Innocentusa at 2007-7-12 10:18:39 > top of Java-index,Java Essentials,Java Programming...
# 5

> 1. But how can I open a channel to a HTTP-Server?

Connect on the port the server is listening to (typically 80).

> 2. And how can I send the HTTP headers?

http://www.w3.org/Protocols/rfc2616/rfc2616.html

> 3. The best would be to wrap the stuff into a new

> Class called "URLConnectionSocket" or like that. So I

> don't need to restructure everything but to replace

> some portions of my uploader-class-code...

Okay.

~

yawmarka at 2007-7-12 10:18:40 > top of Java-index,Java Essentials,Java Programming...
# 6
You mentioned ProgressBar, so I figured GUI ... O-L
abillconsla at 2007-7-12 10:18:40 > top of Java-index,Java Essentials,Java Programming...
# 7

Jakarta's HttpClient might be of use? Never used it.

ProgressMonitorInputStream? Never could figure out how it knows the percentage, though.

> And - no - it is not a GUI-Problem because I output the

> results on the console. And the console is thread-save (yea).

I don't think that is a proper assessment. Apples and oranges... the console and UI aren't related.

There's always the fake progress results way.

bsampieria at 2007-7-12 10:18:40 > top of Java-index,Java Essentials,Java Programming...
# 8

Well - I did a short look on the HTTP-Protocol.

I saw that is theoritically not needed to answer something to the http-server, right?

Well, I just send my multipart-post-stuff over the stream and close the connection again.

I absolutely don't need any response.

So - do I only need that code?:

//...

try {

//Open HTTP-Socket-Connection on HTTP-Port 80:

HTTPSocket = new Socket(toConnect.toString(), 80, true);

InputStream sInStream = HTTPSocket.getInputStream();

//Now pump the stuff (header + Content (multipart-form-data) in to the stream:

//...

//Close the connection again, bye Server, do something with that...

HTTPSocket.close();

} catch (IOException ex) {

ex.printStackTrace();

}

Pls check!

Thank you!

With best regards!

MfG

Inno

Innocentusa at 2007-7-12 10:18:40 > top of Java-index,Java Essentials,Java Programming...
# 9
You read from an InputStream. You write to an OutputStream. Beyond that, if you want to know if your code works, test it!~
yawmarka at 2007-7-12 10:18:40 > top of Java-index,Java Essentials,Java Programming...
# 10

try {

//Open HTTP-Socket-Connection on HTTP-Port 80:

HTTPSocket = new Socket(toConnect.toString(), 80, true);

InputStream sInStream = HTTPSocket.getInputStream();

//Now pump the stuff (header + Content (multipart-form-data) in to the stream:

//...

//Close the connection again, bye Server, do something with that...

if (sInStream != null) sInStream.close(); // Added

HTTPSocket.close();

} catch (IOException ex) {

ex.printStackTrace();

}

abillconsla at 2007-7-12 10:18:40 > top of Java-index,Java Essentials,Java Programming...
# 11
Thank you very much!Another question:How can I write a simple String to an OuptutStream ?I knew it :)Thank you!With best regards!MfGInno
Innocentusa at 2007-7-12 10:18:40 > top of Java-index,Java Essentials,Java Programming...
# 12
> How can I write a simple String to an OuptutStream ?String s = "foo";OutputStream os = getSomeOutputStream();PrintStream out = new PrintStream(os);out.print(s);out.close();~
yawmarka at 2007-7-12 10:18:40 > top of Java-index,Java Essentials,Java Programming...
# 13
OK!But do I need to send "POST" before?The headers are OK.But could you give me an example of a multipart-Post-Header?Thank you very much!With best regards!MfGInno
Innocentusa at 2007-7-12 10:18:40 > top of Java-index,Java Essentials,Java Programming...
# 14
> But could you give me an example of a multipart-Post-Header? http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.2 http://www.ietf.org/rfc/rfc1867.txt~
yawmarka at 2007-7-12 10:18:40 > top of Java-index,Java Essentials,Java Programming...
# 15
The first thing you write is the path, method and version..."/index.html POST HTTP/1.1"Or GET... Or maybe that's backwards. Read the protocol that was mentioned above. However it seems like it's not worth the effort of doing your own HTTP connection class.
bsampieria at 2007-7-21 21:17:54 > top of Java-index,Java Essentials,Java Programming...
# 16
guessContentTypeFromName() - where do I get the source of that function?
Innocentusa at 2007-7-21 21:17:54 > top of Java-index,Java Essentials,Java Programming...
# 17
> guessContentTypeFromName() - where do I get the> source of that function?From the source code distributed with the JDK.~
yawmarka at 2007-7-21 21:17:54 > top of Java-index,Java Essentials,Java Programming...