HTTP Post Request in Objective-C Not Working

前端 未结 1 823
你的背包
你的背包 2021-01-15 03:51

I am writing an HTTP Post request, but for some reason the parameters are not being added correctly, and I can\'t for the life of me figure out what I\'m doing wrong. Here\'

相关标签:
1条回答
  • 2021-01-15 04:35

    Try this

     NSString *Post = [[NSString alloc] initWithFormat:@"Post Parameters"];
        NSURL *Url = [NSURL URLWithString:@"Url"];
    
        NSData *PostData = [Post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    
        NSString *postLength = [NSString stringWithFormat:@"%d", [PostData length]];
    
        NSMutableURLRequest *Request = [[NSMutableURLRequest alloc] init];
        [Request setURL:Url];
        [Request setHTTPMethod:@"POST"];
        [Request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [Request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [Request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [Request setHTTPBody:PostData];
    
    0 讨论(0)
提交回复
热议问题