Ios 如何在http post正文上发布json字符串?在obj c中

Ios 如何在http post正文上发布json字符串?在obj c中,ios,http-post,afnetworking,nsurlconnection,afnetworking-2,Ios,Http Post,Afnetworking,Nsurlconnection,Afnetworking 2,您好,我是ios开发新手,我想通过http post请求在我的web服务上发送一些json数据,数据将在请求体中而不是在参数中 谢谢你的帮助 试试这段代码 NSURL *url = [NSURL URLWithString:@"Your Json URL"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url

您好,我是ios开发新手,我想通过http post请求在我的web服务上发送一些json数据,数据将在请求体中而不是在参数中

谢谢你的帮助

试试这段代码

    NSURL *url = [NSURL URLWithString:@"Your Json URL"];

     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                                   cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                               timeoutInterval:60.0];
            [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
            [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

            [request setHTTPMethod:@"POST"];

            NSURLResponse *response;
            NSError *err;
            NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
            NSString *str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
            NSLog(@"responseData in string : %@",str);

          NSMutableDictionary *dictTemp = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];

NSLog(@"responseData in Dictionary : %@",dictTemp);
可能重复的
> Try this code

    -(void)answerCode
{
    NSError * error ;
    NSURLResponse * urlResponse;

    NSURL * postUrl =[NSURL URLWithString:YourUrl];//enter your url

//your body here 
NSString * body =[NSString stringWithFormat:@"email=dharasis"];



    NSMutableURLRequest * request =[[NSMutableURLRequest alloc]initWithURL:postUrl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:50];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];

    NSData * data =[NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
    if (!data) {

        return;
    }

    id json =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

  NSLog(@"Json result is:%@",josn);

}