как сделать http пост в какао на айфоне - PullRequest
0 голосов
/ 18 апреля 2009

Может кто-нибудь вставить какой-нибудь код о том, как сделать http-пост из нескольких значений?

Ответы [ 3 ]

0 голосов
/ 18 апреля 2009

Вот базовый код, который выполняет вызов POST:

//url is the appropriate url for the http POST call
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
            [theRequest setHTTPMethod:@"POST"];

            NSURLConnection *theConnection = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
            if(theConnection)
            {
                webData = [[NSMutableData data]retain];
            }
            else
            {
                NSLog(@"theConnection is NULL");
            }

Вам необходимо реализовать соответствующие методы делегата NSURLConnection.

0 голосов
/ 06 ноября 2009

// Вы можете управлять NSURLConnection синхронно, используя sendSynchronousRequest: returningResponse: error: // но это заблокирует весь поток до получения ответа

// thebodyData = полезная нагрузка отправлена ​​на сервер (в правильном формате) // theMimeType = mineType полезной нагрузки // url - это подходящий URL для вызова http POST

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
        [theRequest setHTTPMethod:@"POST"];

        NSURLConnection *theConnection = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
        if(theConnection)
        {
            webData = [[NSMutableData data]retain];
             // give the details of the payload -- mine time and body content.
            [theRequest setValue: theMimeType forHTTPHeaderField:@"Content-Type"];
            [theRequest setHTTPBody:theBodyData];

        }
        else
        {
            NSLog(@"theConnection is NULL");
        }

// the delegate methods templates...

 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [webData setLength:0];  // clear the data incase it was a redirect in between.
}

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webData appendData:data];  // collect the data from server as it comes in.
}

 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [[NSAlert alertWithError:error] runModal]; // report the error
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // Once this method is invoked, "webData" contains the complete result
}
0 голосов
/ 18 апреля 2009

Второй ответ в поиске Google выглядит примерно так, как вам нужно:

http://www.iphonedevforums.com/forum/sample-code/69-getting-content-url.html

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...