gpt4 book ai didi

ios - 如何在 NSMutableRequest 中添加请求参数

转载 作者:行者123 更新时间:2023-11-29 02:44:48 24 4
gpt4 key购买 nike

我创建了一个可变请求。

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
// I want to add a request parameter - Not in the header or body.

Add Your code here

//

[request setHTTPBody:data];

任何人请帮助我

最佳答案

希望对你有帮助

NSString *post=[NSString stringWithFormat:@"data={\"user_id\":\"abc\",\"Method\":\"User_nji_abc\"}"];

NSURL *url=[NSURL URLWithString:YOUR_BASE_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/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSOperationQueue *queue=[[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response,
NSData *data, NSError *error) {
if ([data length] >0 && error == nil){


dicWholeValue=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];


NSLog(@"%@",dicWholeValue);

}
else if ([data length] == 0 && error == nil){
NSLog(@"Nothing was downloaded."); }
else if (error != nil){
[[NSOperationQueue mainQueue] addOperationWithBlock:^{

//Do any updates to your label here
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Message" message:@"Could not connect to server." delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];

[alert show];
alert=nil;
NSLog(@"Error happened = %@", error);

}];

} }];

关于ios - 如何在 NSMutableRequest 中添加请求参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25280442/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com