简体   繁体   中英

how to use the HTTP range header in J2ME?

  1. is it possible to use range header in HTTP with HTTP GET request?
  2. if yes than how?

I just want to download bytes from server with specified bytes. ie if I want to download bytes from 0 to 255.

See this sample code,

      HttpConnection connection = null;
      InputStream inputstream = null;
      try
      {
        connection = (HttpConnection) Connector.open(url); // Enter your URL here.
        //HTTP Request
        connection.setRequestMethod(HttpConnection.GET);
        connection.setRequestProperty("Content-Type","//text plain");
        connection.setRequestProperty("Connection", "close");
        // HTTP Response
        System.out.println("Status Line Code: " + connection.getResponseCode());
        System.out.println("Status Line Message: " + connection.getResponseMessage());
        if (connection.getResponseCode() == HttpConnection.HTTP_OK)
        {
          System.out.println(
            connection.getHeaderField(0)+ " " + connection.getHeaderFieldKey(0));        
          System.out.println(
           "Header Field Date: " + connection.getHeaderField("date"));
          String str;
          inputstream = connection.openInputStream();
          int length = (int) connection.getLength();
          if (length != -1)
          {
            byte incomingData[] = new byte[length];
            inputstream.read(incomingData);
            str = new String(incomingData);
          }
          else  
          {
            ByteArrayOutputStream bytestream = 
                  new ByteArrayOutputStream();
            int ch;
            while ((ch = inputstream.read()) != -1)
            {
              bytestream.write(ch);
            }
            str = new String(bytestream.toByteArray());
            bytestream.close();
          }
          System.out.println(str);
        }
      }
      catch(IOException error)
      {
       System.out.println("Caught IOException: " + error.toString());
      }
      finally
      {
        if (inputstream!= null)
        {
          try 
          { 
            inputstream.close();
          }
          catch( Exception error)
          {
             /*log error*/
          }
        }
        if (connection != null)
        {
          try
          {
             connection.close();
          }
          catch( Exception error)
          {
             /*log error*/
          }

You could try this:

HttpConnection httpConnection = (HttpConnection) Connector.open("http://www.server.com/file");
httpConnection.setRequestMethod(HttpConnection.GET);
httpConnection.setRequestProperty("Range","0-255");

Check the docs out: HttpConnection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM