如何在objectivec中使用asp.net中的POST方法



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(

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

使用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];
}

相关内容

  • 没有找到相关文章

最新更新