2014-03-26 2 views
0

아이폰에서 웹 서비스에 대한 사용자 권한을 부여하려고합니다. 동등한 개미 테스트 자바 버전은 다음 코드입니다 :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)); 

객관적인 - 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 "); 


} 

나는 그것이 도움이되기를 바랍니다.