ios HTTP post请求

来源:互联网 发布:冰与火之歌 知乎 编辑:程序博客网 时间:2024/05/21 03:17

NSData *postData = [jsonStringdataUsingEncoding:NSUTF8StringEncodingallowLossyConversion:YES];

    

    NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:[NSURLURLWithString:urlStr]];

    

    [request setHTTPMethod:@"POST"];

    [request setHTTPBody:postData];

    NSString* postDataLengthString = [[NSStringalloc] initWithFormat:@"%d", [postDatalength]];

    [request setValue:@"application/x-www-form-urlencoded"forHTTPHeaderField:@"Content-Type"];

    [request setValue:postDataLengthStringforHTTPHeaderField:@"Content-Length"];

    

    NSError *error =nil;

    NSURLResponse *responses;

    

    NSData *data = [NSURLConnectionsendSynchronousRequest:request returningResponse:&responseserror:&error];

    NSDictionary *responseDic =nil;

    NSDictionary *responseDic = [NSJSONSerializationJSONObjectWithData:dataoptions:NSJSONReadingMutableLeaveserror:nil];


Content-Type 的类型有多种,在此用到了application/x-www-form-urlencoded,这也是一种默认的类型,大家可以搜一下HTTP的Content-Type类型


0 0
原创粉丝点击