HTTP Post

Discussion in 'Questions (Windows Mobile)' started by ohkovar, Jun 27, 2007.

  1. ohkovar

    ohkovar Member Licensed User

    I need to Post some long data to our webserver and I need the server to parse the data as Post variables. Using the HTTP library, I can send GET data easily by appending the variables to the URL (ex. url.php?var1=Y&var2=N).

    Since I have some long data that really can't get sent via the GET method, how can I easily send data using the POST method? I have seen the way the tutorial uses the PUT method and reading from a file, but I am not uploading a file. I just need to send data pairs to the server as POST variables.

    In a normal HTML page, the form would look like this:
    <form>
    <input type="hidden" name="var1" value="Y">
    <input type="hidden" name="var2" value="N">
    <input type="submit" value="submit">
    </form>

    How can I accomplish the same thing with PPC?

    Here is what I have now, but it doesn't work:

    **********************
    Request.New1("http://www.url.com") 'Build the request.
    Request.Method = "POST"
    Request.TimeOut = 30000 '30 seconds
    Response.New1

    Writer.New1(Request.GetStream,true)
    Writer.WriteString("<input type='hidden' name='tet' value='im alive'>")
    Response.Value = Request.GetResponse 'Call the server
    result = Response.GetString 'Get the string from the response stream.
    Response.Close
    ************************

    Thanks for all the help. You guys are great!
     
  2. ohkovar

    ohkovar Member Licensed User

    I figured it out. Simple solutions can sometimes take forever to find! Here is the solution, just in case anyone wants to know:

    *********************
    Response.New1
    Request.New1(URL)
    Request.Method = "POST"
    Request.ContentType = "application/x-www-form-urlencoded"
    bin.New1(Request.GetStream,true) 'Use a BinaryFile object to write the data to the Request stream.
    bin.WriteString("var1=Y&var2=N")
    Response.Value = Request.GetResponse
    result = Response.GetString
    Response.Close
    msgbox(result)
    **************************

    The content-type was required. With that in place, just write the variables in name=value pairs to the GetStream using the BinaryFile library. The webserver you post to will reconize them as posted variables, accessible by the name assigned to the variable.

    The page on my webserver (in PHP) just returns the variables that are posted:

    <?php
    foreach($_POST as 4p=>$v){
    echo $p." - ".$v;
    }
    ?>
     
  3. ohkovar

    ohkovar Member Licensed User

    One other note:

    For some reason, a weird character is placed in front of the GetStream. I guess this may be something that gets written to start the file? Anyway, a quick fix (since it is late and I am getting tired) is to just put a dummy variable at the start of your name/value pairs (ex. bin.WriteString("dummyvar=2&var1=Y&var2=N")). Then just ignore the first dummy variable and move on to the good ones!
     
  4. rmh

    rmh Member Licensed User

    The example GET code in the help works OK for me using URL variables but I also need to send volume data and having struggled with this for some time, I discover the code below works on pc but -not- on device.... (WM5)

    I get a response, but no data is sent.

    this is using BinaryFile.dll version 1.2

    Sub Globals

    End Sub

    Sub App_Start
    Form1.Show
    Response.New1
    Request.New1(URL)
    Request.Method = "POST"
    Request.ContentType = "application/x-www-form-urlencoded"
    bin.New1(Request.GetStream,true) 'Use a BinaryFile object to write the data to the Request stream.
    st="var1=z&var2=hullo world2"
    bin.WriteString(st)
    Response.Value = Request.GetResponse
    result = Response.GetString
    Response.Close
    msgbox(result)
    End Sub
     
Loading...