如何在NSMutableRequest中添加请求参数 [英] How to add request Parameters in NSMutableRequest

查看:449
本文介绍了如何在NSMutableRequest中添加请求参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个可变请求。

  NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url 
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[请求setHTTPMethod:@POST];
[请求setValue:@application / jsonforHTTPHeaderField:@Content-Type];
//我想添加一个请求参数 - 不在标题或正文中。

在此处添加您的代码

//

[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];
[请求setHTTPMethod:@POST];
[请求setValue:postLength forHTTPHeaderField:@Content-Length];
[请求setValue:@application / x-www-form-urlencodedforHTTPHeaderField:@Content-Type];
[request setHTTPBody:postData];
NSOperationQueue * queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:请求队列:队列completionHandler:^(NSURLResponse *响应,
NSData *数据,NSError *错误){
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 download。); }
else if(error!= nil){
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {

//这里对你的标签做任何更新
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@Message消息:@无法连接到服务器。 delegate:nil cancelButtonTitle:nil otherButtonTitles:@OK,nil];

[提示显示];
alert = nil;
NSLog(@错误发生=%@,错误);

}];

}}];


I Have created a mutable request .

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];

Any one please help me

解决方案

HOPE THIS WILL HELP YOU

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);

        }];

    } }];

这篇关于如何在NSMutableRequest中添加请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆