2014-03-26 93 views
0

我正在嘗試在iphone中對webservice進行用戶授權。等效的螞蟻測試Java版本是以下代碼:iphone - Authorization Post請願

HttpPost post = new HttpPost("http://webserviceurl/authuser"); 

    // Header Basic base64 user:pass 
    post.setHeader("Authorization", "Basic " + Base64.encodeBase64String(StringUtils.getBytesUtf8("myuser:mypassword"))); 

    // FIELD 
    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); 
    urlParameters.add(new BasicNameValuePair("usuario", "[email protected]")); 
    urlParameters.add(new BasicNameValuePair("password", "pass")); 

    post.setEntity(new UrlEncodedFormEntity(urlParameters)); 

如何使相同的objective-c? 我solutios是:

NSString *urlString = @"http://webserviceurl/authuser"; 
NSURL *url = [NSURL URLWithString:urlString]; 

NSString *userName = @"myuser"; 
NSString *password = @"mypass"; 

NSError *myError = nil; 

// create a plaintext string in the format username:password 
NSMutableString *loginString = (NSMutableString*)[@"" stringByAppendingFormat:@"%@:%@", userName, password]; 

// employ the Base64 encoding above to encode the authentication tokens 
NSString *encodedLoginData = [Base64 encode:[loginString dataUsingEncoding:NSUTF8StringEncoding]]; 


// create the contents of the header 
NSString *authHeader = [@"Basic " stringByAppendingFormat:@"%@", encodedLoginData]; 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url 
                cachePolicy: NSURLRequestUseProtocolCachePolicy 
                timeoutInterval: 5]; 


// add the header to the request. 
[request addValue:authHeader forHTTPHeaderField:@"Authorization"]; 

// perform the reqeust 
NSURLResponse *response; 

NSData *data = [NSURLConnection 
       sendSynchronousRequest: request 
       returningResponse: &response 
       error: &myError]; 

// webserver's response. 
NSString *result = [Base64 base64StringFromData:data length:64]; 

「編碼」 和 「base64StringFromData」 是一個的外部類的方法Base64編碼(如這裏link1

是我的代碼嗎? 如何獲得服務器響應? 而我該如何實現java「FIELD」?

任何建議真的很感激。 Tx提前

回答

0

我剛剛解決,我想發佈它,如果有人有相同的需要。

我的代碼是:

-(IBAction)buttonEntra:(id)sender{ 

     NSLog(@"starting autorization"); 
     NSString *urlString = @"http://sytes.net:8080/rest/checkuser"; 
     NSURL *url = [NSURL URLWithString:urlString]; 

     NSString *serverName = @"serverUSER"; 
     NSString *serverPassword = @"serverPASS"; 

     // create a plaintext string in the format username:password 
     NSString *authStr = [NSString stringWithFormat:@"%@:%@", serverName, serverPassword]; 

     NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding]; 
     NSString *authValue = [NSString stringWithFormat:@"Basic %@", [Base64 encode:authData ]]; 

     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url 
                   cachePolicy: NSURLRequestUseProtocolCachePolicy 
                  timeoutInterval: 10]; 
     [request setHTTPMethod:@"POST"]; 
     [request setValue:authValue forHTTPHeaderField:@"Authorization"]; 

     //parametros en el body 

     NSString *userName = usuario; 
     NSString *userPassword = password; 

     //esempio [email protected] - sicignano 
     NSString *post = [NSString stringWithFormat: @"usuario=%@&password=%@",userName,userPassword]; 
     NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
     [request setHTTPBody:postData]; 


     // perform the reqeust 
     NSURLResponse *response; 
     NSError *myError = nil; 

     NSData *data = [NSURLConnection 
         sendSynchronousRequest: request 
         returningResponse: &response 
         error: &myError]; 

     NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
     [connection start]; 
    } 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    //status code 200 OK - status code 405 no method defined 
    NSLog(@" connection didReceiveResponse: %@", response); 

} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d { 

    NSString *string = [[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding]; 
    NSLog(@"connection didReceiveData: %@",string); 

} 



- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    NSLog(@"didFailWithError: %@ ", [error localizedDescription]); 

    [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Servicio no disponible", @"") 
           message:@"El servidor podria encontrarse en mantenimiento" 
           delegate:nil 
           cancelButtonTitle:NSLocalizedString(@"OK", @"") 
           otherButtonTitles:nil] show]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 

    // Do anything you want with it 
    NSLog(@"connectionDidFinishLoading "); 


} 

我希望它能幫助。