简体   繁体   中英

How to POST & GET JSON data from Objective c to web service C#?

Currently i am facing an issue like :: I have an some parameters like (UserName=Brahmam&Passowrd=123&Mobileno=98989898&emailid=Brahmam@gmail.com) and already my .net developer created API using ( C# with response is JSON format only).

Here I uploaded my source code:

-(void)registestion{
 NSString * apiURLStr =[NSString stringWithFormat:@"http://example.com/api/v1/example?UserName=Brahmam&Passowrd=123&Mobileno=98989898&emailid=Brahmam@gmail.com"];
 NSMutableURLRequest *dataRqst = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:apiURLStr] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
 [dataRqst setHTTPMethod:@"POST"];
 NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
 NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
 [dataRqst addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];
 NSMutableData *postBody = [NSMutableData data];
 [dataRqst setHTTPBody:postBody];
 NSHTTPURLResponse *response =[[NSHTTPURLResponse alloc] init];
 NSError* error;
 NSData *responseData = [NSURLConnection sendSynchronousRequest:dataRqst returningResponse:&response error:&error];
 NSString *responseString = [[NSString alloc] initWithBytes:[responseData bytes] length:[responseData length] encoding:NSUTF8StringEncoding];
 NSLog(@"%@",responseString);
}

If i run this API usin Browser : the response is

  • {"status":"1","msg":[{"id":112,"Name":"Brahmam","Password":"123","MobileNo":"98989898","Emailid":"Brahmam@gmail.com"}]}

But i am unable to post date from objective-c to C# web service:: I tried to use above code but i am getting response like:

  • {"Message":"The requested resource does not support http method 'POST'."}

Can any one help me out Plz..

Use the below matter

NSString * apiURLStr =[NSString stringWithFormat:@"http://taxi.expertverification.com/api/v1/RegistrationAPI?UserName=Brahmam&Passowrd=123&Mobileno=98989898&emailid=Brahmam123444@gmail.com"];
NSMutableURLRequest *dataRqst = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:apiURLStr] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
NSHTTPURLResponse *response =[[NSHTTPURLResponse alloc] init];
NSError* error = [[NSError alloc] init] ;
NSData *responseData = [NSURLConnection sendSynchronousRequest:dataRqst returningResponse:&response error:&error];
NSString *responseString = [[NSString alloc] initWithBytes:[responseData bytes] length:[responseData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",responseString);
  NSURL *main_Url = [NSURL URLWithString:baseUrl];
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:main_Url];
    [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    request.HTTPMethod = @"POST";
    NSError *error = nil;
    NSData *Postdata = [NSJSONSerialization dataWithJSONObject:dictParameter options:0 error:&error];
    if (!error) {
        NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
                                        fromData:Postdata completionHandler:^(NSData *Pdata,NSURLResponse *response,NSError *error) {
            if(!error) {
                   _onSuccess(Pdata);
            } else{
                _onError(error);
            }

        }];
        [uploadTask setTaskDescription:@"DatPasted"];
        [uploadTask resume];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM