HttpURLConnection simply doesn't work as it should!

Hi everybody. I've been stuck for a week with these related problems. answers to any of them would be very, very appreciated (i'm working in Java 1.2.2)
I am sending a POST request from a client applet to a Web server (a CGI made in Perl). I have checked the server code: it's all right. When I try to send a POST message I do this at the client:
URL url = new URL(Direccion);
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setDoInput(true);
http.setDoOutput(true);
http.setUseCaches(false);
http.setRequestMethod("POST");
http.setRequestProperty ("Content-Encoding","multipart/form-data");
http.setRequestProperty ("Content-Type","multipart/form-data; boundary=" + sep);
Then I connect:
http.connect();
System.out.println("Client : Connected");
Then supposedly I send a message in HTTP format:
DataOutputStream DOSSalida = new DataOutputStream (http.getOutputStream ());
System.out.println("Client : Writing Content");
DOSSalida.writeBytes(sContent); //post message
DOSSalida.writeBytes(sEnd); //post end of transmission
System.out.println("Client : Flushing Stream");
DOSSalida.flush();
DOSSalida.close();
I send file contents on the message; I mean, usually is a large post (most than 1 Mb).
Question 1: At this point, the message is supposed to be sent, isn't it? But, hey, check this out: the message is not really sent yet! (I mean, I have a log for the web server and the CGI: nothing happens). �Shouldn't the message have been sent at this moment? Shouldn't I even be able to close the connection (http.close()) at this time and the message should have been sent? (I have even tried with the http.setDoInput(false) set).
Question 2: The message is really sent (I can't see why) when I do the getInputStream (!!) code.
System.out.println("Client : Waiting for response from Server");
AreaInformacion.setForeground(new java.awt.Color(0, 0, 0));
AreaInformacion.setText("Estado: archivos enviados. Esperando respuesta");
System.out.println ("Creamos el amortiguador.\n");
BufferedReader in = new BufferedReader(new InputStreamReader(http.getInputStream())); // <- Here it is where the message seems to be really being sent!!
// The answer is received too (that's OK)
System.out.println("Client : Opened input stream");
String input = "", response = "";
while((input = in.readLine()) != null)
response += input + "\r";
System.out.println("Client : received : "+response);
The message is sent, as I was saying, and an answer is always, at least, sent from the server.
Question 3: Even further, I am having a third problem. Sometimes the getInputStream line seems to hang. Look: the message arrives fine to the web server, a message from thre web server to the client is received but the program keeps stuck on the getInputStreamLine. Why??
Question 4: For the previous problem (the important one), after craving and poring over this forum, I found a message that seems to explain that sometimes HttpURLConnection hangs (GetInputStream) (http://forum.java.sun.com/thread.jsp?forum=4&thread=347082). I implemented a threaded solution for when the getInputStream hangs:
1) The main process open the getInputStream in a different thread.
2) The main process gets asleep for X seconds.
3) The child process opens the GetInputStream
4) The child process "awakes the main process.
5) Everythings keep going on.
This works: if the GetInputStream method gets stuck (step 3), its parent kill him. I show the client a fancy web page, equal to the one received if getInputStream wouldn't have stuck.
Nonetheless, if the message sent is too long (remember: they're long posts, usually 5 mb. or more long) it can happen that the parent kills the thread befor the message is sent (the message is sent with the getInputStream, remember?) So the process is killed before it really gets stuck and the fancy page telling that the message has been sent says nothing but lies!
Would you please help me? Excuse me, i have tried to do the shortest possible.
Thanks a lot.
Regards,
Mario R.

HttpURLConnection wasnt designed to send large chunks of data
part of a POST request is a header "Content-length", which can only be calculated when all the data has been assembled in a buffer (before anything is sent), must be that flushing and closing the OutputStream is not what the programmers use as a signal that all data has been declared (by you)
I would either use a Socket and work out th Http bit my self or write a CGI script that compiles the data in smaller chunks (~1k blocks), you could do this in a temp file on the server

Similar Messages

  • My iPod Nano doesn't work as it should...

    I have just got my new iPod Nano. It's the 2gb model from the second generation. I was extremely happy, because it looks awesome and I thought that it would work same way.
    The player is working perfect. Plays songs, really awesome.
    But I have really BIG problems with iTunes. As I said I have got iPod today and everything is new and up-to-date - iTunes and I even somehow managed to update my iPod. Why somehow? I have extremely big problems with updating it. I think that it works okay now, but not at all. Everything is okay when I use an automatic system. I copy music I want to library, conect iPod then and everything copies perfect. The problems are starting when I try to manage iPod on my own - copy music etc. This simply doesn't work! When I try to change the system on non-automatic the error (-50) occurres and iPod cannot be updated. I even can't do this simple thing so there are no talks about deleting or copying music to iPod.
    What can I do? That's simply driving me crazy :/
    PS. Sorry for my English, but I'm from Poland. I don't know if the apple service exists here at all...

    And I have one more question, when I connect my iPod,
    iTunes turns on automatically and the iPod is seen,
    but it's not seen in "My Computer". Is that ok?
    Since you have the iPod set at auto sync the ipod would not show in "My Computer" (someone correct me if I am wrong). If you were able to successfully switch it to manual it would show as it would be in disk mode. So I would say that this is ok.
    I have done a search for the error -50 you keep getting and can't find anything. At this point I would suggest contacting Apple support to see if they can give you some insight on this error. One thing though...have you tried uninstalling iTunes from your computer completely and then re-installing it to make sure it's not an issue with your current copy of iTunes? Also, have you tried to go through the 5 R's?
    http://www.apple.com/support/ipod/five_rs/
    If you have not gone through the 5 R's, please try that first and see if it helps. If not, then try un-installing and then re-installing iTunes. If that does not work, then it would probably be beneficial to call Apple Support.

  • Backup Assistant Plus simply doesn't work, how do I disable it?

    I have a shiny new Samsung Galaxy S III and when I set up my contacts, I added them as "Phone" entries with the expectation that the Backup Assistant Plus app would allow me to back them up. Except that it doesn't and, from what I've seen online, it is a miserable piece of junk that simply doesn't work for many people.
    Soooo....
    I sent all my contacts as a big V-card to my Gmail account, sorted things out in Gmail, unjoined them all on the phone, and deleted the Phone entries. Now I only have Gmail contact entries and they synch with no issues.
    My only question now is, how do I disable/stop Backup Assistant Plus so that it doesn't constantly give me notifications about how incapable it is? I've tried clearing it's data and forcing it to stop. But it comes back from the dead, like a demonic zombie! It appears the only thing the app can do is resurrect it itself for the sole purpose of notifying me that it will fail to do anything useful.
    Any and all help with this issue is greatly appreciated.

    greblaws wrote:
    I have been trying to disable Backup Assistant Plus since it is a BATTERY and PERFORMANCE HOG!  I have a lowly Droid3, and it has been really causing issues.
    Your instructions above DO NOT WORK, under Manage Applications I get "Uninstall updates" - I cannot uninstall it. It is a "system application" and I am locked out of that control.  GRRRR.  Clear data does not do anything for performance.
    Even when on wifi, this app's backup of data is slower than TAR in January.  Biggest issue is the
    The reason the steps to uninstall the app didn't work is because the DROID 3 is running Gingerbread (Android 2.3).  The instructions above were for the Samsung Galaxy S III running either ICS (Android 4.0) or Jelly Bean (Android 4.1).
    You can however "Clear the Data" for the app and from that point never use the app.  The phone will learn to not load it into the RAM in case you ever use it.

  • HT4527 How on earth do you get Home Sharing to work properly?  We have tried multiple times and it simply doesn't work for us!

    How on earth do you get Home SHaring to work?  We have tried numerous times and it simply doesn't work for us!

    Turn it on.
    Honestly, if you want help it is generally a good idea to ask a specific question and give some details of what problem is occurring.
    If trying to move media, using home sharing is simply a bad idea.  Follow some of the other instructions in the article from which the question was posted to move media.

  • Since I downloaded the last updated, the app PODCAST doesn't work. Wha should I do guys?

    Since I downloaded the last updated, the app PODCAST doesn't work. Wha should I do guys?

    it didnt work. I also erase the podcast that I has and reinstall it

  • My touchscreen doesn't work as it should.

    My touchscreen doesn't work as it should. I can't touch the field / Selektion I like to, 'cause the screen is trembling and scrolling without any direct touch from me. It even reacts when my finger is about 1-2 cm away from the screen. The ipod didn't fall down or had any other accident. It's an iPod 16gb 2nd generation. What can I do?

    That was my first thought. I even put off the shield which I had stucked on to protect the iPod from dirt and scratches. But it stays the same.
    But I deleted some apps which I had installed yesterday a few minutes ago, maybe it will work now... Hopefully...

  • HT201320 My iPad doesn't recognize my email account.  When I delete it and reenter it still doesn't work.  What should I do?

    My ipad doesn't recognize my email account.  When I delete the account and reenter it, it still doesn't work.  What should I do?

    Close All Open Apps...  Perform a Reset... Try again...
    Reset  ( No Data will be Lost )
    Press and hold the Sleep/Wake button and the Home button at the same time for at least ten seconds, until the Apple logo appears. Release the Buttons.
    http://support.apple.com/kb/ht1430
    More Info Here  >  http://www.apple.com/support/ipad/mail/

  • Brand New everything but just doesn't work as it should

    Hello,
            Just bought a £2000 HP Elitebook 2.8Ghz with 3gb ram, XP, and a lot a bells and whistles. Installed CS4 production Premium and then the updates for Premiere, Encore, Photoshop and Soundbooth.
    Started a new project, connected my Panasonic HVX200 to import DVCPROHD 1080i50 footage - simple you would think.
    Wont do it, locks up everytime.
    Now I started using Premiere on version 5.1 many years ago but stopped using it on version 6 after getting sick of its crashing and instability.
    I went over to FCP and it was a breath of fresh air to say the least.
    When Premiere Pro first came out, I tested it on a brand new laptop, crashed within the first minute and that was the end of that !
    After 5 years on virtually flawless FCP use I thought I'd try out Premiere again and guess what, nothings changed, it just doesn't work as it should.
    How anyone on these forums makes any money from it and keeps their sanity is beyond me.

    They must have corrected this oversight. I tried it out with the following TXT file, and indeed it no longer worked:
    There is no egg in eggplant nor ham in hamburger; neither apple nor pine in pineapple. English muffins weren't invented in England nor French fries in France. Sweetmeats are candies while sweetbreads, which aren't sweet, are meat. Quicksand works slowly, boxing rings are square and a guinea pig is neither from Guinea nor is it a pig. Why is it that writers write but fingers don't fing, grocers don't groce and hammers don't ham?
    If the plural of tooth is teeth, why isn't the plural of booth beeth? One goose, 2 geese. So one moose, 2 meese? Doesn't it seem crazy that you can make amends but not one amend. If you have a bunch of odds and ends and get rid of all but one of them, what do you call the thing left? Is it an odd, or an end?
    If teachers taught, why didn't preachers praught? If a vegetarian eats vegetables, what does a humanitarian eat? In what language do people recite at a play and play at a recital? Ship by truck and send cargo by ship? Have noses that run and feet that smell?
    How can a slim chance and a fat chance be the same, while a wise man and a wise guy are opposites? You have to marvel at the unique lunacy of a language in which your house can burn up as it burns down, in which you fill in a form by filling it out and in which an alarm goes off by going on.
    English was invented by people, not computers, and it reflects the creativity of the human race, which, of course, is not a race at all. That is why, when the stars are out, they are visible, but when the lights are out, they are invisible.
    What a pity. I just hope that the queueing is taking less time than in the past. I will look at your info ASAP and report back.

  • Cursor  doesn't work as it should

    When I scroll or simply move the cursor it just doesn't want to listen to me most of the time. It jumps a little when  I'm not anywhere near the trackpad, it jumps, doesn't move, moves slowly and jagged movements. all randomly happens at random times.
    I've done all the 3 steps suggested  through  "trouble shooting" ( I forget what it was called) here on the site with no luck.
    I have a feeling it is not a hardware problem as sometimes when I wisit certain websites it can get worse.
    But then again sometimes it's totally unaffected and works like it should.
    I don't know if this is related, but after 3 days with this annoyance, Finder keeps asking me for a password to make changes when I log on. I haven't done anything but to log on to provoke it.
    I do have firevault on if it has any relevance to either issue.
    Thanks for any guidance!

    That was my first thought. I even put off the shield which I had stucked on to protect the iPod from dirt and scratches. But it stays the same.
    But I deleted some apps which I had installed yesterday a few minutes ago, maybe it will work now... Hopefully...

  • IChat in Lion simply doesn't work

    I have a 2008 Mac Pro that has been through Leopard, Snow Leopard and now Lion upgrades.
    And a 2011 MacBook Pro that saw an upgrade to Lion.
    I cannot get iChat to work on either machine. Both have been transported to several different locations (hence different ISP's and router configurations), I have tried deleting and re-adding my AIM account on both machines as well. Have tried SSL on and off, Wifi, Ethernet connections. Nothing seems to work.
    Username and password are obviously fine. It simply doesn't change from "offline". I have tried changing via Status, via iChat > Log In, nothing. No messages, no error messages, its like it doesn't even realize I'm giving it a command. Have also tried changing port settings, connecting without a router at all, etc. I'm very confident it is a software issue/bug, not something with my connection. As it worked just fine before the Lion upgrade (on both machines)
    Any suggestions?

    Hi,
    Thanks for the Points.
    The steps were listed in the least Destructive first.
    They also served to show that it could be one or two causes.
    The IMAgent issue has the possibility of messing up Face Time as well.
    The Keychain issue could cause you issues with several apps like the Address book and Mail as well as Face Time and iChat due to the interlinking created by the Address Book use of your Contacts' info.
    Although the last does and is sometimes the only course of action it is relatively extreme and lacks a certain finesse.
    Doing all three without checking in between is a bit over the top in my book, and misses out of the staged learning I was trying to also get across.
    Glad to hear you have it working.
    8:49 PM      Thursday; October 6, 2011
    Please, if posting Logs, do not post any Log info after the line "Binary Images for iChat"
     G4/1GhzDual MDD (Leopard 10.5.8)
     MacBookPro 2Gb( 10.6.8)
     Mac OS X (10.6.8),
    "Limit the Logs to the Bits above Binary Images."  No, Seriously

  • When I open my camera app, the camera doesn't open. I got told to restore my iPhone and I have done but it still doesn't work? What should I do?

    When I open my camera app it doesn't seem to open the actual camera. I went into an apple store and got told to restore it so I have done but it still doesn't open. What should I so about it?

    When you restored, did you restore as a "new" device or from backup? If you restored as new, and still have the same issue, you most likely have a hardware problem. Go back to the Apple store to confirm.

  • Gmail Widget in Dashboard Simply Doesn't Work

    Hi,
    I just purchased a MacBook Pro. Everything is great, but for some reason the Gmail widget in dashboard simply will not work. It installs, but after I put in my email and password the gmail logo blinks and the window remains blank. I have tried downloading it and uninstalling it numerous times. This is a really useful widget as it will allow me to check my email at a glance and it is simply not working. Am I missing something?
    Thanks,
    Ryan

    im wondering if its a compatibility issue with 10.5.6? // i cant try it out now but i will later tonight /// try using mail untill then as long as you leave it open then it will put numbers on the dock icon and beep once when e-mail comes in.

  • 1st generation; hasn't been updated; simply doesn't work.

    This isn't a 3.0 issue, btw. Got the iPod from a friend. I press the home button, sometimes either the low battery icon will appear, sometimes the Apple logo. Same thing with the menu button. I try to reset it, that doesn't work either. Doesn't even register on my computer, won't charge on the A/C adapter. I have no clue what is wrong with this thing.
    Quick update--as of tonight, the iPod is not responding to anything at all. Completely blank screen.
    Message was edited by: DxS-Phreak

    Reset: Hold the Sleep/Wake and Home buttons and don’t let go until the screen goes dark and the Apple logo appears (no data will be lost)

  • It simply doesn't work at all

    Quite strange: I have DW CS4 Build 4117 German on Mac OS X
    10.5.5. InContext doesn't work at all! Between "Spry" and "Data
    Objects" there is no "InContext". The manual and documentation
    explain everything detailed. I also downloaded the inContext
    tuturial, but nothing works! On the German Adobe webiste inContext
    is also advertised as included in DW CS4. So what's wrong? Am I
    missing something?
    Regards,
    Andi

    Thanks! I solved this issue by uninstalling and reinstalling
    DW CS4. It seemed like one of these typical Adobe problems when a
    beta was installed before. I had the English DW CS4 beta installed
    and just installed the German version without uninstalling the
    beta. Now everything works fine - quite. A lot of times when
    finishing any CS4 app the Adobe crash reporter pops up ;-)

  • What (I think) doesn't work as it should in iTunes 10

    1- No streaming from my iTunes library to my iPhone while in my home network! Seriously? I can move, listen and even copy songs from one computer to other, but I cannot listen to whatever is on my iTunes library from my iPhone while I'm at home? Why??? Or am I wrong and you can do it? Maybe I'm just not seeing it...
    2- The "Like" feature of Ping could be something to have from the contextual menu in the library itself, instead of going to the store simply to do it. I know, some of the tracks I have are not in the store, but the ones that do should have that feature? How hard could it be?
    3- Not a big one, but I kind of like to have control over my album cover sizes, not just three fixed ones.
    Anyway, I like the grey look The only thing colored is the covers, which makes it pop as it should.

    I'm sorry, I meant the sfml package from the community repositories, not from the AUR.
    Here's the problem:
    If I compile the following C++ code, using the facile sans font :
    #include <SFML/Graphics.hpp>
    #include <iostream>
    using namespace std;
    int main()
    sf::RenderWindow window(sf::VideoMode(500, 500), "Example");
    sf::Font font;
    font.loadFromFile("Facile Sans.ttf");
    sf::Text text;
    text.setFont(font);
    text.setPosition(0, 0); // (0, -20) looks good
    text.setCharacterSize(84);
    text.setString("Text");
    text.setColor(sf::Color::Black);
    while (window.isOpen())
    sf::Event event;
    while (window.pollEvent(event))
    if (event.type == sf::Event::Closed)
    window.close();
    window.clear(sf::Color::White);
    window.draw(text);
    window.display();
    return 0;
    The output looks like this on linux:
    If I compile the same code on windows, it looks like this:
    It seems like that, for some reason, some space is left on top of the text, so that characters like "ê" can be displayed correctly. The weird thing is, that this problem didn't occurr to the other users in the SFML forums, whether they were using fedora or arch. Also, it worked for me, after I reinstalled Linux Mint 17 and built SFML from source directly, without installing the libsfml-dev package. Now this problem occurrs on my freshly installed arch linux, although I built SFML from source directly. In order to fix my issue, I tried to uninstall SFML and install the sfml package from the community repositories instead, with no luck; so I uninstalled the sfml package with yaourt -Rs sfml or pacman -Rs sfml  (I don't remember which command I used).

Maybe you are looking for