如何在目标c中的asp.net web api方法中使用POST方法?

Posted

技术标签:

【中文标题】如何在目标c中的asp.net web api方法中使用POST方法?【英文标题】:How to use POST method in asp.net web api method in objective c? 【发布时间】:2016-09-16 09:57:06 【问题描述】:

ASP.NET Web API 应用程序定义了 post 方法来筛选使用复杂视图模型数据的 userDetails。

.h

#define URL "http://101.127.236.85:6067/tmsservice/MobileService.svc/AddTimeSheet"

.m

- (void)viewDidLoad 



 [super viewDidLoad];

    [self sendRequestWithPostType];



- (void) sendRequestWithPostType

    NSString *temp=[NSString stringWithFormat:@URL];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:temp]];


    [request addValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

    [request addValue:@"ios" forHTTPHeaderField:@"DeviceType"];

    [request addValue:@"123456" forHTTPHeaderField:@"DeviceID"];

    [request addValue:@"05e2954e-9ba6-4ab3-84af-5793b55bc69f" forHTTPHeaderField:@"Authorization"];

    [request setHTTPMethod:@"POST"];



    [request addValue:@"0"forHTTPHeaderField:@"TimeSheetID"];

    [request addValue:@"16/9/2016" forHTTPHeaderField:@"EntryDateString"];// Selected Entry Date

    [request addValue:@"4" forHTTPHeaderField:@"EmployeeID"];

    [request addValue:@"Ram Kadam" forHTTPHeaderField:@"EmployeeName"];

    [request addValue:@"70"forHTTPHeaderField:@"ProjectID"];

    [request addValue:@"Azure Discovery" forHTTPHeaderField:@"ProjectCode"];

    [request addValue:@"Azure Discovery" forHTTPHeaderField:@"ProjectName"];

    [request addValue:@"3"forHTTPHeaderField:@"ActivityID"];

    [request addValue:@"Prototype" forHTTPHeaderField:@"ActivityName"];

    [request addValue:@"8.5" forHTTPHeaderField:@"Hours"];

    [request addValue:@"Under UAT Testing" forHTTPHeaderField:@"WorkDone"];

    [request addValue:@"2"forHTTPHeaderField:@"Status"];


    [request setHTTPMethod:@"POST"];

    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    NSLog(@"conn:= %@",conn);




- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response



    responseData = [[NSMutableData alloc]init];




- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error



  NSLog(@"Did Fail");




- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data




    [responseData appendData:data];


- (void)connectionDidFinishLoading:(NSURLConnection *)connection



  NSLog(@"Did Finish");

    jsonObject=[NSJSONSerialization
                JSONObjectWithData:responseData
                options:NSJSONReadingMutableLeaves
                error:nil];

    NSLog(@"jsonObject is %@",jsonObject);


回应:-

网址:http://101.127.236.85:6067/tmsservice/MobileService.svc/AddTimeSheet

完成了

jsonObject 为 (null)

请告诉我这段代码哪里错了。

【问题讨论】:

请重新格式化您的问题 @Shriram Kadam 不要使用 nsurl 连接,它已被贬低,使用 Nsurlsession 您好 Imran,我如何使用 Nsurlsession?你能告诉我或在我的代码中重新输入吗? 有朋友帮帮我吗?如何使用带有 JSON 参数的 POST 方法和 Using Nsurlsession? 【参考方案1】:

使用 NSURLSession 进行 POST,这里是一个例子:

NSURL *url = [NSURL URLWithString:@"http://101.127.236.85:6067/tmsservice/MobileService.svc/AddTimeSheet"];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
// your header key-values
config.HTTPAdditionalHeaders = @@"key1": @"value1";
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = @"POST";

// your body key-values
NSDictionary *dictionary = @@"key1": @"value1";
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary
                                               options:kNilOptions error:&error];
if (!error) 
    NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
                                                               fromData:data completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) 
                                                                   // Handle response here
                                                               ];
    [uploadTask resume];

【讨论】:

以上是关于如何在目标c中的asp.net web api方法中使用POST方法?的主要内容,如果未能解决你的问题,请参考以下文章

Web API系列教程2.1 — ASP.NET Web API中的路由机制

ASP.NET Web API 中的自定义方法名称

如何将int数组发送到post web api c#asp.net [重复]

如何在Asp .Net Web API中的请求的整个生命周期中使类对象可用?

如何从 ASP.NET 核心 mvc 向 asp.net 核心中的 Web API 发出 PUT 请求?

如何在asp.net核心中从客户端调用web api方法?