×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    29

    KVM crashes when reading content with HTTP "302 Found" response on N90 and 6680

    Hello
    I found that a MIDlet will silently die on Nokia N90 and 6680 when trying to read the content of an HTTP "302 Found" server response.
    Here is a small code showing it :

    Code:
    import javax.microedition.midlet.*;
    import javax.microedition.io.*;
    import java.io.*;
    import javax.microedition.lcdui.*;
    
    public class Test
    extends MIDlet
    {
      private static final String URL = "http://server.domain/your_script.php"
      
      protected void startApp() throws MIDletStateChangeException
      {
        Form f = new Form( "Test" );
        String response="";
        try {
          HttpConnection con = 
            (HttpConnection)Connector.open( URL, Connector.READ );
          InputStream in = con.openInputStream();
          InputStreamReader reader = new InputStreamReader(in);
          char content[] = new char[1024];
          int len = reader.read( content, 0, 1024 );
          reader.close();
          in.close();
          response = String.valueOf(content,0,len);
        }
        catch( IOException e ) {}
        f.append( new StringItem("Reponse : ", response, StringItem.PLAIN) );
        Display.getDisplay(this).setCurrent(f);
      }
    
      protected void pauseApp() {}
    
      protected void destroyApp(boolean arg0) throws MIDletStateChangeException{}
    }
    And here is the server-side PHP code to generate a HTTP 302 reply with content :
    Code:
    <?php
    header('Location: http://www.forum.nokia.com/' );
    echo 'plop';
    ?>
    It was REALLY hard to find out why my much more complicated app kept on crashing...
    Isn't that part of the HTTP standard to send a content together with a 302 reply ?

  2. #2
    Regular Contributor
    Join Date
    Jun 2003
    Posts
    73

    Re: KVM crashes when reading content with HTTP "302 Found" response on N90 and 6680

    hey, i feel your pain, 6630/6680 crashes too, n70 does not , but conenction still can not get through, any idea?

  3. #3
    Super Contributor
    Join Date
    Dec 2005
    Location
    Europe/Poland/Warsaw
    Posts
    1,699

    Re: KVM crashes when reading content with HTTP "302 Found" response on N90 and 6680

    hi,

    shouldn't getResponseCode/getResponseMessage of HTTPConnection be used to handle that situation correctly?

    regards,
    Peter

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    29

    Re: KVM crashes when reading content with HTTP "302 Found" response on N90 and 6680

    peterblazejewicz, getResponseCode() would return (int)302 and getResponseMessage() "302 Found".
    What I wanted was the actual content of the HTTP reply (i.e. what comes after the reply headers).

    I had the developer of the web service change his API to let the MIDlet connect but this is a workaround. Even if a 302 HTTP reply means that the content has moved to another URI specified in the Location: header, it seems to me that some content can be sent together (and actual implementations allow it, e.g. Apache 2 for the server-side and Nokia 3230 for client-side).

    See http://www.w3.org/Protocols/rfc2616/...html#sec10.3.3 for info on "302 Found" HTTP reply.

  5. #5
    Super Contributor
    Join Date
    Dec 2005
    Location
    Europe/Poland/Warsaw
    Posts
    1,699

    Re: KVM crashes when reading content with HTTP "302 Found" response on N90 and 6680

    hi Emmanuel,

    well, I was under impression (remember that I'm still learner) that correct action is to:
    - bypass read headers/content routine
    - close current connection
    - reopen current conenction using Location header to request correct data,
    it's based on sample I've used some time ago:
    Code:
    private HttpConnection open(String url) throws IOException {
        HttpConnection c;
        int status = -1;
    
        // Open the connection and check for redirects
        while (true) {
          c = (HttpConnection) Connector.open(url);
    
          // Get the status code,
          // causing the connection to be made
          status = c.getResponseCode();
    
          if ((status == HttpConnection.HTTP_TEMP_REDIRECT)
              || (status == HttpConnection.HTTP_MOVED_TEMP)
              || (status == HttpConnection.HTTP_MOVED_PERM)) {
    
            // Get the new location and close the connection
            url = c.getHeaderField("location");
            c.close();
          } else {
            break;
          }
        }
    
        // Only HTTP_OK (200) means the content is returned.
        if (status != HttpConnection.HTTP_OK) {
          c.close();
          throw new IOException("Response status not OK");
        }
        return c;
      }
    and notes from MIDP docs:
    302: The requested resource resides temporarily under a different URI. (Note: the name of this status code reflects the earlier publication of RFC2068, which was changed in RFC2616 from "moved temporalily" to "found". The semantics were not changed. The Location header indicates where the application should resend the request.)
    that's I've learned so far but could be wrong of course,
    hth,
    regards,
    Peter

  6. #6
    Regular Contributor
    Join Date
    Jun 2003
    Posts
    73

    Re: KVM crashes when reading content with HTTP "302 Found" response on N90 and 6680

    keep watching the thread , hope to get a way to handle 302 on s50 properly....

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    29

    Re: KVM crashes when reading content with HTTP "302 Found" response on N90 and 6680

    Hi Peter,

    I understand that a web browser should get the right URI from the Location header and open a new connection to that URI. In that case, that was not the behavior I was interested in.

    The web service I wanted to connect would actually redirect me to some XHTML advanced profile content intended for a full-featured PC browser (i.e. FireFox, IE, etc), content that could not be used on a mobile phone, BUT the body of the reply would also contain a simple integer telling weither authentication had succeeded or failed and if the session cookie was valid for further use of the web service. Also the redirection URI was always the same and could not be used to guess what happened.

    So I continue to think it's an implementation issue, even if I agree that the HTTP 302 return code is INTENDED to be used for temporary redirection, because the protocol is actually DESIGNED to allow other behaviors.

    Regards,

    Manu

  8. #8
    Super Contributor
    Join Date
    Dec 2005
    Location
    Europe/Poland/Warsaw
    Posts
    1,699

    Arrow Re: KVM crashes when reading content with HTTP "302 Found" response on N90 and 6680

    Hi Emmanuel,

    I think I'm correct with a way we should handle redirection,
    however on real device (I own 6680) that seems to be impossible as you found,

    for example:
    Code:
    c = (HttpConnection) Connector.open(url);
                get_mainForm().append("Request url:\n");
                get_mainForm().append(url);
                status = c.getResponseCode();
                get_mainForm().append("response:\n");
                get_mainForm().append("code: "+c.getResponseMessage()+"\n");
                get_mainForm().append("message: "+c.getResponseCode()+"\n");
                if (status == HttpConnection.HTTP_MOVED_TEMP ||
                        status == HttpConnection.HTTP_MOVED_PERM ||
                        status == HttpConnection.HTTP_TEMP_REDIRECT) {
                    url = c.getHeaderField("location").trim();
                    get_mainForm().append("Redirecting to " + url+"\n");
                    c.close();
                    c = null;
                    // other code goes here 
                }
    everythings work fine in emulators (Suns WTK/Nokia SDKs),
    but as soon as I tried that or similiar code on real device (6680 here) after calling:
    Code:
    url = c.getHeaderField("location").trim();
    that thread is dead and frozen (midlet is responding fine and is working),
    it does not throw any exception or something, just probably similiary to your finding its stuck forever (unitl midlet is closed),
    I've tried with url which returns 302 status code:
    Code:
    String url = "http://www.s60.com/";
    and as mentioned that works very fine on emulators,
    can someone test that URL, it should nicely redirect to http://www.s60.com/life and it does that in emulators
    I've also tried OperaMini and it nicely redirects but I'm not sure if redirection is not handled on server side in OperaMini implementation,

    regards,
    Peter

  9. #9
    Regular Contributor
    Join Date
    Jun 2003
    Posts
    73

    Re: KVM crashes when reading content with HTTP "302 Found" response on N90 and 6680

    Peter,
    pretty sure that opera mini does the redirection on server side

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    1

    Re: KVM crashes when reading content with HTTP "302 Found" response on N90 and 6680

    Hi all,

    Trying to get my browser code running on a 6680, I encounter the same troubles as described above.

    Redirecting should provide a Location field, but as I enumerate all headers in the first response it's all empty (null)! Any emulator (not a S60) will provide the correct Location field.

    Peter, as I see in your code:

    Code:
    url = c.getHeaderField("location").trim();
    this statement will cause a NullPointerException. Is that the reason is seams to be frozen?

    Do you also face this null header fields?

    Regards,
    Frans

  11. #11
    Regular Contributor
    Join Date
    Jun 2003
    Posts
    73

    Re: KVM crashes when reading content with HTTP "302 Found" response on N90 and 6680

    aha, seems we guys in this thread are all in browser related biz, care to share your applications? web sites maybe?

  12. #12
    Super Contributor
    Join Date
    Dec 2005
    Location
    Europe/Poland/Warsaw
    Posts
    1,699

    Re: KVM crashes when reading content with HTTP "302 Found" response on N90 and 6680

    hi Frans,

    yes, that's correct, I've later wrote a code for test 6680 without using automatic redirection but simplyl to test response code and test header "location", its null, althouth it works correct on emulators (nokia/sun wtk),
    that's the reason I guess of some issues decribed earlier,
    I've simply will not use redirection at all unless I get some other devices for tests,
    However I have non-latest firmware version and not sure if that is related (latest is 5.0.*.* released that year),
    http://www.newlc.com/Nokia-Series-60...story,933.html
    it could be a bug in api implementation or some firmware issue, not sure,

    @steven
    I've simply using HttpConenctions mainly for REST services research like some public apis: google/yahoo/flickr/etc (web 2 api) for client side applications without server side tier and client side parsing with either thing-xml parsers or jsr-172 parser (which my device does not support though), I think it bill be quite common way of sharing/accesing data in future

    regards,
    Peter

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    24

    Re: KVM crashes when reading content with HTTP "302 Found" response on N90 and 6680

    Hi.
    I can confirm the problem in a Nokia 6630.
    It also happens to me in the project I'm working.

    In the following piece of code (all inside a loop to resolve the
    redirections) the output of getHeaderField("Location") is null
    when the status is 302.
    Code:
           // Get the status code, causing the connection to be made
           status = connection.getResponseCode();
           log("HTTPstatus: " + status);
           if (status == HttpConnection.HTTP_TEMP_REDIRECT ||
                   status == HttpConnection.HTTP_MOVED_TEMP ||
                   status == HttpConnection.HTTP_MOVED_PERM) {
               // Get the new location and close the connection
               url = connection.getHeaderField("Location");
               connection.close();
               log("Redirecting to " + url);
           } else {
               // no redirect
               break;
           }
    I've tried with "location" and "Location" with the same result.

    Like the rest of us it works fine under the emulator.

    Are there any way to get all the header fields? May be the info
    is somewhere else.

    -Hernán.

    PS: Thanks Peter, I've followed your advise and am using
    NetBeans right now.

  14. #14
    Registered User
    Join Date
    Apr 2006
    Posts
    24

    Re: KVM crashes when reading content with HTTP "302 Found" response on N90 and 6680

    Does this recipe apply here too?
    http://discussion.forum.nokia.com/fo...ead.php?t=6196

    Should we have parse response headers ourselves?
    Ick!!!

    I've simply will not use redirection at all unless I get some other devices for tests,
    Lucky you!
    The Google site I'm pointing at always redirect me.

    -H.

  15. #15
    Super Contributor
    Join Date
    Dec 2005
    Location
    Europe/Poland/Warsaw
    Posts
    1,699

    Re: KVM crashes when reading content with HTTP "302 Found" response on N90 and 6680

    Hello Hernan,

    Quote Originally Posted by hfoffani
    Are there any way to get all the header fields?
    here you are:
    Code:
      public void run() {
            HttpConnection connection = null;
            int response = -1;
            StringBuffer buffer = new StringBuffer("");
            char lf = '\n';
            try {
                connection = (HttpConnection) Connector.open("http://www.forum.nokia.com/");
                buffer.append("host: ").append(connection.getHost()).append(lf);
                response = connection.getResponseCode();
                if(response == HttpConnection.HTTP_OK){
                   buffer.append("response: 200").append(lf);
                   // get headers
                   int i = 0;
                   String key = null;
                   while((key = connection.getHeaderFieldKey(i++)) != null){
                       buffer.append("key: ").append(key).append(lf);
                       buffer.append("value: ").append(connection.getHeaderField(key)).append(lf);
                   }
                }else{
                   buffer.append("failure: ").append(response).append(lf);
                }
            } catch (IOException ex) {
                buffer.append("exception:").append(lf);
                buffer.append(ex.getMessage()).append(lf);
            } finally{
                if(connection != null){
                    try {
                        connection.close();
                    } catch (IOException ex) {}
                }
            }
            mainScreen.setString(buffer.toString());
            buffer = null;
        }
    which should results in (add out.println if you prefer):
    host: www.forum.nokia.com
    response: 200
    key: date
    value: Tue, 15 Aug 2006 18:53:04 GMT
    key: server
    value: Apache
    key: accept-ranges
    value: bytes
    key: set-cookie
    value: ND=2; path=/
    key: set-cookie
    value: ND=2; path=/
    key: content-type
    value: text/html; charset=UTF-8
    key: connection
    value: close
    (run via Sun 2.5 WTK Emulator)

    PS: Thanks Peter, I've followed your advise and am using
    NetBeans right now.
    you're welcome

    hth
    regards,
    Peter

Page 1 of 2 12 LastLast

Similar Threads

  1. Play video downloaded over HTTP on 6680 and N90
    By Emmanuel Raulo in forum Mobile Java Media (Graphics & Sounds)
    Replies: 2
    Last Post: 2006-02-07, 08:29

Posting Permissions

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