×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    4

    Add username and password to an HTTP connection with J2ME

    ¿How can I do this?
    Some sites need authentication through an username and password. In Internet Explorer you can do it with this URL:
    http://loginassword@server
    But it doesn't work with J2ME.

    MIDP documentation tells you can do it with HttpConnection class through the setRequestProperty method, but it's needed to do it before connect. The one way I know to use an HttpConnection is through these sentences:

    HttpConnection c = null;
    c = (HttpConnection)Connector.open(url);

    So connection is done inmediately and setRequestProperty cannot be used.

    Some tutorials tell it can be done this way:

    HttpConnection c = null;
    c = (HttpConnection)Connector.open(url);
    c.setRequestProperty("...");

    They tell it works because the connection is opened when doing c.openInputStream(), but it doesn't work, an error occurs telling the connection is open.

    Anyone know the solution?
    Best regards
    David Aldavero

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    2
    This is how i add username&pass through HTTP connection

    HttpConnection mConn = (HttpConnection)Connector.open(urlAddr);

    mConn.setRequestMethod(HttpConnection.POST);
    mConn.setRequestProperty("IF-Modified-Since",
    "20 Jan 2001 16:19:14 GMT");
    mConn.setRequestProperty("User-Agent",
    "Profile/MIDP-1.0 Configuration/CLDC-1.0");
    mConn.setRequestProperty("Content-Language", "en-CA");
    mConn.setRequestProperty("Connection","close");
    //---------------------------------------------------
    mConn.setRequestProperty("User",usr);
    mConn.setRequestProperty("pass",pass);
    //------------------------------------------------------

    It worked just fine. On the Server u read the User & pass parameter with HttpResponse.getHeader("..") if u're using a Servlet in the Server

    Hope it help

Posting Permissions

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