×

Discussion Board

Results 1 to 3 of 3

Hybrid View

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    19

    J2ME HTTP Post problem

    Hi

    I ran into a problem while trying to establish HTTP Post request to the server on Nokia 6100 and Nokia 3510i and I guess that the same will happend on the other Nokia handsets.

    This problem doesn't occure on the emulator only on the handset and it seems to work fine with HTTP Get requests.

    Attached the source code that was used to check this porblem.

    Did anyone ran into the same problem ?

    public void performLogin()
    {
    HttpConnection conn = null;
    InputStream is = null;
    OutputStream os = null;
    byte[] receivedData = null;
    try
    {
    String url =
    "http://www24.brinkster.com/partneroggi/HTTPtest.asp";
    // "http://192.168.238.36/MTEServer/HTTPtest.asp";
    conn = (HttpConnection)Connector.open(url);
    conn.setRequestMethod(HttpConnection.POST);
    conn.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
    conn.setRequestProperty("Content-type","application/x-www-form-urlencoded");
    byte[] postData = createPostData();
    // In the current version of J2ME, when COntent-length
    // is set using setRequestProperty(), the length is
    // sometimes transmitted as two numbers separted by
    // commas This causes the servlet container to throw a
    // NumberFormatException
    //conn.setRequestProperty("Content-length",
    // Integer.toString(postData.length));
    os = conn.openOutputStream();
    os.write(postData);
    // In the current version of J2MEWTK, flush() sometimes
    // throws an exception. Since the openInputStream() that
    // follows flushes the buffer, flush() is commented out
    //os.flush();
    is = conn.openInputStream();
    String contentType = conn.getType();
    int len = (int)conn.getLength();
    if (len > 0)
    {
    receivedData = new byte[len];
    int nb = is.read(receivedData);
    }
    else
    {
    receivedData = new byte[1024];
    int ch;
    len = 0;
    while ((ch = is.read()) != -1)
    {
    receivedData[len++] = (byte)ch;
    }
    }
    response.setText(new String(receivedData,0,len));
    display.setCurrent(outputForm);
    }
    catch (IOException e)
    {
    System.out.println(e.getMessage());
    e.printStackTrace();
    }
    finally
    {
    try
    {
    if (is != null)
    {
    is.close();
    }
    if (os != null)
    {
    os.close();
    }
    if (conn != null)
    {
    conn.close();
    }
    }
    catch (IOException e)
    {
    }
    }
    }

    public byte[] createPostData()
    {
    StringBuffer sb = new StringBuffer();
    sb.append("userid=");
    sb.append(userid.getString());
    sb.append("&password=");
    sb.append(password.getString());
    System.out.println("sb = <" + sb.toString() + ">");
    return sb.toString().getBytes();
    }

  2. #2
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    Make sure the POST request is reaching the server intact. If you are connecting via a gateway then the gateway could be changing the request headers, and even cutting off the content(This happens a lot when using POST). Try setting up a sniffer on your server and see that what you send is really what you receive.

    shmoove

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    19
    Hi,

    To check if my gateway can work with HTTP post I used Webcab.de post check (url: http://webcab.de/pc.wml) and the test seems to pass.

    Anymore ideas ?

    Thanks,
    Gil

Posting Permissions

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