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
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%
> 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].~
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
> 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.
~
You mentioned ProgressBar, so I figured GUI ... O-L
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.
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
You read from an InputStream. You write to an OutputStream. Beyond that, if you want to know if your code works, test it!~
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();
}
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
> 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();~
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
> 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~
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.
guessContentTypeFromName() - where do I get the source of that function?
> guessContentTypeFromName() - where do I get the> source of that function?From the source code distributed with the JDK.~