1

誰かがこのクエリで私を助けてください。

iPhone (ios5) で .SVC ファイルを含む URL から JSON データを取得する方法は?

リンクは次のようなものです: http://156.160.45.118/api/Login.svc?wsdl (オリジナルではありません)

パラメーターは次のとおりです。電子メールとパスワード。

では、ログイン資格情報を確認するにはどうすればよいですか?

私のコード:

NSString *username = emailField.text;
 NSString *password = passwordField.text;
 NSMutableDictionary *dictionnary = [NSMutableDictionary dictionary];
 [dictionnary setObject:username forKey:@"user_email"];
 [dictionnary setObject:password forKey:@"user_password"];

 NSError *error = nil;
 NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionnary
 options:kNilOptions
 error:&error];   

 NSString *urlString = @"http://156.160.45.118/api/Login.svc?wsdl";
 NSURL *url = [NSURL URLWithString:urlString];

 // Prepare the request
 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
 [request setHTTPMethod:@"POST"];
 [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
 [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
 [request setValue:@"json" forHTTPHeaderField:@"Data-Type"];
 [request setValue:[NSString stringWithFormat:@"%d", [jsonData length]]  forHTTPHeaderField:@"Content-Length"];
 [request setHTTPBody:jsonData];    

 NSError *errorReturned = nil;
 NSURLResponse *theResponse =[[NSURLResponse alloc]init];
 NSData *data = [NSURLConnection sendSynchronousRequest:request
 returningResponse:&theResponse
 error:&errorReturned];
 if (errorReturned) 
 {
 //...handle the error
 }
 else 
 {
 NSString *retVal = [[NSString alloc] initWithData:data
 encoding:NSUTF8StringEncoding];
 NSLog(@"%@", retVal);

 }
4

1 に答える 1

1

最後に、多くのことを調査することで答えを得ました。

NSString *username = emailField.text;
        NSString *password = passwordField.text;
        NSMutableDictionary *dictionnary = [NSMutableDictionary dictionary];
        [dictionnary setObject:username forKey:@"user_email"];
        [dictionnary setObject:password forKey:@"user_password"];

        NSError *error = nil;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionnary
                                                           options:kNilOptions
                                                             error:&error];   

        NSString *urlString = http://156.160.45.118/api/Login.svc/login;

        NSURL *url = [NSURL URLWithString:urlString];
 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        [request setHTTPMethod:@"POST"];

        [request setHTTPBody:jsonData];
        NSURLResponse *response = NULL;
        NSError *requestError = NULL;
        NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
        NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] ;
         NSLog(@"%@", responseString); 
于 2012-07-09T10:20:47.347 に答える