将Post Request发送到服务器

时间:2012-04-12 14:17:55

标签: iphone objective-c ios web-services

我有这个PHP代码。

<?php  
       $users = $this->Session->read("user");
       $varFName = $users['User']['first_name'];
       $varLName = $users['User']['last_name'];
       $message = "Hello,
 I would like to invite you to Clicksays! ".$varFName." ".$varLName;
       echo $this->Form->create('User', array('id' => 'sendEmailRequest', 'style'=>'padding : 0px;', 'url' => array('controller' => 'Contacts', 'action' => 'addEmailRequest')));
       echo $this->Form->input('Email', array('id' => 'email', "size"=>"40"));
       echo $this->Form->textarea("Message", array("type"=>"textarea", "id"=>"PreviewEmail", "style"=>"display:none;","rows"=>"5", "cols"=>"45", "value"=>$message));
       echo $this->Form->button('Preview and edit invitation Clickup', array("id" => "butInvite", "type"=>"button"));
       echo $this->Form->button('Send invitation Clickup', array('id' => 'butSendInvitation',  'type'=>'submit'));
       echo $this->Form->end(); ?>

现在它接受用户,电子邮件和消息的数据数组:

NSURL *URL = [NSURL URLWithString:@"myLink"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:6.0];
[request setHTTPMethod:@"POST"];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
    [connection start];

现在我很确定我需要将数组作为HTTP主体,但我不知道如何。我不是一个网络人,对网络编程和方法知之甚少。

我如何传递细节?没有它它不起作用,并发布错误输入用户的详细信息,该代码也存在于PHP中。

2 个答案:

答案 0 :(得分:0)

RestKit非常适合处理iOS应用中的JSON内容。查看本教程:http://mobile.tutsplus.com/tutorials/iphone/restkit_ios-sdk/

答案 1 :(得分:0)

尝试这个

    NSString *urlString = @"Your url";

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    NSMutableData *body = [NSMutableData data];


    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];




    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];



    // Text parameter1
    // NSString *param1 = @"parameter text";
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"name\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:param1] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];



    // Another text parameter
    // NSString *param2 = @"Parameter 2 text";
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"desc\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:param2] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];


    // close form
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    // set request body
    [request setHTTPBody:body];
像上面的代码一样,你可以使用HTTP post方法发送多个值。