Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2011
    Posts
    6
    Rep Power
    0

    Question how do i write url connection in the loop?

    i have this method which sends binary data to server

    the code works fine, but still, it's structure is kinda stupid:

    output and input connections are opened like thousands times (depends on data size), closed only once.

    i cannot move the code that opens connection outside of the loop 'cause i'm getting strange errors, like the data was never sent to server.

    please help me to restructure this code so it could still work and have a correct structure:

    Java Code:
    OutputStream os = null;
    StringBuffer messagebuffer = new StringBuffer();
    HttpURLConnection huc = null;
    DataInputStream dis = null;
    InputStream in = null;
    Path path = null;
    byte[] buf = new byte[64 * 1024];
    int bytesRead = 0;
    
    try {
      path = Paths.get("D:\\testfile.rar");
      in = Files.newInputStream(path);
    
      int i = 0;
      URL u = new URL(defaultURL);
    
      while ((bytesRead = in.read(buf)) != -1) {
    
        huc = (HttpURLConnection) u.openConnection(); // wrong 
        huc.setRequestMethod("POST");
        huc.setRequestProperty("chunk-number", i + "");
        huc.setDoOutput(true); // wrong 
        huc.setDoInput(true); // wrong 
        os = huc.getOutputStream(); // wrong 
        os.write(buf, 0, bytesRead);
        os.flush();
        System.out.println(i++ + " " + bytesRead);
    
        int ch;
        dis = new DataInputStream(huc.getInputStream()); // wrong 
    
        long len = huc.getContentLength();
        if (len != -1) {
          for (int k = 0; k < len; k++)
            if ((ch = dis.read()) != -1)
              messagebuffer.append((char) ch);
            else {
              // if the content-length is not available
              while ((ch = dis.read()) != -1)
                messagebuffer.append((char) ch);
              }
        }
        Thread.sleep(16);
      }
      dis.close();
      huc.disconnect();
    
      int statusCode = huc.getResponseCode();
      String message = huc.getResponseMessage();
      messagebuffer.append("status code=" + statusCode + "\n");
      messagebuffer.append("response message=" + message + "\n");
    
    } catch (Exception ex) {
      // throw errors 
    } finally {            
      // closing stuff 
    }
    return messagebuffer.toString();

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: how do i write url connection in the loop?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: how do i write url connection in the loop?

    Can you make a version of the code so it compiles and executes for testing? A SSCCE.

  4. #4
    Join Date
    Dec 2011
    Posts
    6
    Rep Power
    0

    Default Re: how do i write url connection in the loop?

    Quote Originally Posted by Norm View Post
    Can you make a version of the code so it compiles and executes for testing? A SSCCE.
    this code is fully functional. just place it inside method, like

    Java Code:
    private String sendPost() { 
      // place it here 
    }
    P.S. Yes, i've posted on Stackoverflow, but the people back there only care about reputation and haven't helped me at all

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: how do i write url connection in the loop?

    just place it inside method
    Ok, I'll wait until someone takes care of making code that is executable.
    That requires import statements and a class and a main etc

Similar Threads

  1. Replies: 7
    Last Post: 11-30-2011, 07:58 PM
  2. Replies: 1
    Last Post: 10-01-2011, 03:12 AM
  3. Java.net.socket connection :connection closed
    By veeru541 in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2010, 03:14 AM
  4. Replies: 3
    Last Post: 04-13-2010, 04:43 PM
  5. Replies: 7
    Last Post: 10-20-2009, 11:34 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •