Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

HELP - Executing a HTTP URL with POST variables ?

TheGameTheGame Posts: 46Registered Users
Is it possible in iphone SDK to execute HTTP URL with POST variables?
Just like in HTML form, in action attribute i have there my HTTP URL and
I have two input components that will add using POST methond in the URL.
If I get it the right URL with POST variables I will get XML data from server.

I really need your help guys. Thanks in advance..
Post edited by TheGame on

Replies

  • TheGameTheGame Posts: 46Registered Users
    Dont mind how the XML data get from the server. I have it already.. I need only to execute the HTTP URL with POST variables in my iphone app.

    Thank you so much in advance..
  • dicklacaradicklacara Posts: 123Registered Users
    Have a look at the NSURLRequest class in the API docs.

    In particular

    – HTTPBody
    – HTTPMethod

    For example:


    NSMutableString *httpBodyString;
    NSURL *url;
    NSMutableString *urlString;

    httpBodyString=[[NSMutableString alloc] initWithString:@\"Name=The Big Bopper&Subject=Hello Baby&MsgBody=...You knooow what I like...Chantilly lace...\"];
    urlString=[[NSMutableString alloc] initWithString:@\"http://www.somedomain.com/contactform.php\"];

    url=[[NSURL alloc] initWithString:urlString];
    [urlString release];

    NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:url];
    [url release];

    [urlRequest setHTTPMethod:@\"POST\"];
    [urlRequest setHTTPBody:[httpBodyString dataUsingEncoding:NSISOLatin1StringEncoding]];
    [httpBodyString release];

    NSURLConnection *connectionResponse = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

    if (!connectionResponse)
    {
    NSLog(@\"Failed to submit request\");
    }
    else
    {
    NSLog(@\"--------- Request submitted ---------\");
    NSLog(@\"connection: %@ method: %@, encoded body: %@, body: %a\", connectionResponse, [urlRequest HTTPMethod], [urlRequest HTTPBody], httpBodyString);
    NSLog(@\"New connection retain count: %d\", [connectionResponse retainCount]);
    responseData=[[NSMutableData data] retain];
    NSLog(@\"response\", responseData);
    }



    HTH

    Dick
  • TheGameTheGame Posts: 46Registered Users
    dicklacara;28889 said:
    Have a look at the NSURLRequest class in the API docs.

    In particular

    – HTTPBody
    – HTTPMethod

    For example:


    NSMutableString *httpBodyString;
    NSURL *url;
    NSMutableString *urlString;

    httpBodyString=[[NSMutableString alloc] initWithString:@\"Name=The Big Bopper&Subject=Hello Baby&MsgBody=...You knooow what I like...Chantilly lace...\"];
    urlString=[[NSMutableString alloc] initWithString:@\"http://www.somedomain.com/contactform.php\"];

    url=[[NSURL alloc] initWithString:urlString];
    [urlString release];

    NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:url];
    [url release];

    [urlRequest setHTTPMethod:@\"POST\"];
    [urlRequest setHTTPBody:[httpBodyString dataUsingEncoding:NSISOLatin1StringEncoding]];
    [httpBodyString release];

    NSURLConnection *connectionResponse = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

    if (!connectionResponse)
    {
    NSLog(@\"Failed to submit request\");
    }
    else
    {
    NSLog(@\"--------- Request submitted ---------\");
    NSLog(@\"connection: %@ method: %@, encoded body: %@, body: %a\", connectionResponse, [urlRequest HTTPMethod], [urlRequest HTTPBody], httpBodyString);
    NSLog(@\"New connection retain count: %d\", [connectionResponse retainCount]);
    responseData=[[NSMutableData data] retain];
    NSLog(@\"response\", responseData);
    }



    HTH

    Dick

    Thanks for the reply, I will check this out.
  • bikrbikr Posts: 272Registered Users
    You could always do it as simple as stringWithContentsOfURL and pass a url with variables in it like

    http://test.com/index.php?test=1&test2=2


    Or do it in an xmlparser if you need to parse the results.. Either will do the job..

    If you don't want the data in plain text, you can just send the block w/ method post using the above.
    RPSOnline - Online Rock Paper Scissors!!

    *Updates*

    2.0 is "For Sale"

    3.0 is "In Development"
Sign In or Register to comment.