sending a post from iOS app to php script not working… Simple solution is likey

后端 未结 3 1386
名媛妹妹
名媛妹妹 2021-01-01 01:16

I have done this several times before but for some reason I can\'t get the post to go through... I tried the php script with the variables set to _POST and without... When t

3条回答
  •  一向
    一向 (楼主)
    2021-01-01 02:15

    Use following code. make sure that post string's receipt is a key and will be used in server. client side code

     NSString *receipt1 = @"username";
    
        NSString *post =[NSString stringWithFormat:@"receipt=%@",receipt1];
       NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    
        NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    
        NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
        [request setURL:[NSURL URLWithString:@"http://localhost:8888/validateaction.php"]];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];
    
        NSHTTPURLResponse* urlResponse = nil;
        NSError *error = [[NSError alloc] init];
        NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
        NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        NSLog(@"Response Code: %d", [urlResponse statusCode]);
    
        if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300)
        {
            NSLog(@"Response: %@", result);
        }
    
    }
    

    server side php script.

    validation.php

    
    

提交回复
热议问题