×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    30

    Multiple requests with HTTPConnection

    Hello,

    I want to send 2 requests to a web server using HTTPConnection. My code runs on my N95 with Python 1.4.0.

    Here is my code :

    Code:
    try:
      params = urllib.urlencode({ 'loginButton': 'Login', 'wui_target_id': 'loginButton', 'wui_event_id': 'onclick', 'username' : options_ui.login, 'password' : options_ui.password })
      headers = { "Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain" }
      conn = httplib.HTTPConnection("www.orange.ch:80")
      conn.request("POST", "/footer/login/loginForm", params, headers)
      response = conn.getresponse()
      cookie = response.getheader('set-cookie')
      #data = response.read()
      
      params = urllib.urlencode({ 'wui_target_id': 'sendButton', 'wui_event_id': 'onclick', 'destinationNumberInput': destinataire[1], 'messageInput' : message_ui.message.get(), 'signatureInput' : '' })
      headers = { "Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain", 'Cookie' : cookie }
      conn.request("POST", "/myorange/sms/smsForm", params, headers)
      response = conn.getresponse()
      #data = response.read()
      
      conn.close()
    
      appuifw.note(u"Message envoyé !")
    except:
      appuifw.note(u"Erreur : message non envoyé")
    It logs me to my provider's page, then send a SMS through their service.

    My questions :

    - Do I have to call response.read() after each requests, even if I don't need the resonse data ? My code works without it, but it is written on Python doc that you must have read the whole response before you can send a new request to the server (http://docs.python.org/lib/httpconnection-objects.html). What is the proper way to do ?

    - When my code is run on my N95, it automatically asks me which access point to use for the first request, then it RE asks me for the second one, although I'm using the same conn object. What am I doing wrong ?

    Thanks for your help.

    Ludovic

  2. #2
    Super Contributor
    Join Date
    May 2006
    Location
    Oulu, Finland
    Posts
    622

    Re: Multiple requests with HTTPConnection

    Salut Ludovic and welcome to the forum

    I guess that
    Code:
    cookie = response.getheader('set-cookie')
    reads the response. Try to remove this line and check if you have errors.

    To avoid getting the access point prompt for every request you can set the default access point check 8.2.2 from the doc. That should work with urllib:
    Code:
    Example 1:
    #access point is selected from the list
    apid = select_access_point()
    apo = access_point(apid)
    set_default_access_point(apo)
    s = socket(AF_INET, SOCK_STREAM)
    print apo.ip()
    s.connect((’www.sourceforge.net’,80))
    s.send(’GET /\r\n\r\n’)
    s.recv(100)
    s.close()
    apo.stop()
    LFD
    Devices:
    Nokia E61 3rd Edition - pys60 1.4.0

    Tips and modules:
    http://www.lfdm.net/thesis

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    30

    Re: Multiple requests with HTTPConnection

    Hi,

    in fact, the line
    Code:
    cookie = response.getheader('set-cookie')
    read the parameter 'set-cookie' from the header of the response. I need that cookie informations for the 2nd request to send my SMS. Maybe, as you said, this line reads also the data, but if I remove it, I don't have cookies anymore...

    I will try to use sockets instead of HTTPConnection, but, for what I want to do, I think sockets are a little bit too "low level"... I would prefer using HTTPConnection. By using sockets I would have to manually extract headers and cookies from the received data.

    Thanks

    Ludovic
    Last edited by tinkywinky; 2007-07-11 at 16:15.

  4. #4
    Super Contributor
    Join Date
    May 2006
    Location
    Oulu, Finland
    Posts
    622

    Re: Multiple requests with HTTPConnection

    HTTPConnection is written in Python and uses sockets, I just checked

    So you can set the default access point!

    LFD
    Devices:
    Nokia E61 3rd Edition - pys60 1.4.0

    Tips and modules:
    http://www.lfdm.net/thesis

Similar Threads

  1. One socket for multiple http requests
    By tiansun in forum Mobile Java Networking & Messaging & Security
    Replies: 3
    Last Post: 2006-08-16, 20:39
  2. multiple flush() not work for j2me httpconnection?
    By tiansun in forum Mobile Java Networking & Messaging & Security
    Replies: 3
    Last Post: 2006-08-15, 18:59
  3. How to handle multiple asynchronous requests ?
    By sri_nainala in forum Symbian Networking & Messaging (Closed)
    Replies: 0
    Last Post: 2005-02-23, 04:08
  4. Replies: 2
    Last Post: 2004-09-16, 10:13
  5. Replies: 3
    Last Post: 2003-07-09, 16:28

Posting Permissions

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