Sunday, March 3, 2013

POST


NSString *text = @"dfkdsfkgdfklkgdfhgh";
   
    NSString *sendTextMessage = [NSString stringWithFormat:@"https://api.vk.com/method/wall.post?owner_id=%@&access_token=%@&message=%@", idUser, tok, [self URLEncodedString:text]];
    NSLog(@"sendTextMessage: %@", [self URLEncodedString:sendTextMessage]);

    NSURL* url = [NSURL URLWithString:sendTextMessage]; // куда отправлять
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";

    [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

Отправить POST-запрос не так тяжело как кажется. Достаточно подготовить «правильный» NSURLRequest.
NSString* params = @"param=value&number=1"; // задаем параметры POST запроса
NSURL* url = [NSURL URLWithString:@"http://server.com"]; // куда отправлять
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
request.HTTPBody = [params dataUsingEncoding:NSUTF8StringEncoding]; // следует обратить внимание на кодировку

// теперь можно отправить запрос синхронно или асинхронно
[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

No comments:

Post a Comment