2014-03-27 5 views
1

에 대한 사용자 이름과 암호를 권한을 부여하는 방법이 jave의 나머지 웹 서비스 아래포스트 자바 나머지 서비스 여기

http://10.222.0.100:8080/CRM/rest/user/login?id=amit&password=amit123 // demo 

에 대한 사후 방법을 사용하여 공격을 시도하고있는 URL 것은 내가 서비스

을 칠 사용하여 코드
NSError* error; 
NSURL* url = [NSURL URLWithString:[self makeURLStringForRequest]]; //http://10.222.0.100:8080/CRM/rest/user/login 
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 

    if (self.requestDict!=nil) 
    { 

NSData* dataToSet = [NSJSONSerialization dataWithJSONObject:self.requestDict options:NSJSONWritingPrettyPrinted error:&error];// dict:{ id:"amit" , password="amit123" } 
if (error==nil) 
{ 
    [request setHTTPBody:dataToSet]; 
    [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[dataToSet length]] forHTTPHeaderField:@"Content-Length"]; 
} 
else 
{ 
    NSLog(@"%@",error); 

} 
    } 
    error=nil; 
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
{ 
    if (error==nil && [data length]>0) 
    { 
     [[Data_Manager getInstanse]processData:data withRequestType:self.requestType andDelegate:self.delegate]; 
    } 
    else 
    { 
      NSLog(@"%@",error); 
     if ([self.delegate respondsToSelector:@selector(didfailWithError:)]) 
     { 
      [self.delegate didfailWithError:error]; 
     } 
    } 
}]; 
} 

하지만 항상 실패로 돌아가는 사람은 누구나 코드에 무엇이 잘못된지 알 수 있습니다.

브라우저에서 전체 URL을 누르면 사실로 되돌아갑니다.

http://10.222.0.100:8080/CRM/rest/user/login?id=amit&password=amit123 // demo 

답변

1

Objective-C의 RESTful API와 상호 작용하기 위해 UNIRest를 사용하는 것이 좋습니다. 당신은 여기 그것을 얻을 수 있습니다 https://github.com/Mashape/unirest-obj-c 프로젝트에 추가 한 후

, 당신은이 같은 요청을 보낼 수 있습니다

[[UNIRest post:^(UNISimpleRequest *simpleRequest) { 
    [simpleRequest setUrl:@"http://10.222.0.100:8080/CRM/rest/user/login"]; 
    [simpleRequest setHeaders:@{@"accept": @"application/json"}]; 
    [simpleRequest setParameters:@{@"id": @"amit", @"password": @"amit123"}]; 
}] asJsonAsync:^(UNIHTTPJsonResponse *jsonResponse, NSError *error) { 
    NSLog(@"%@", [[jsonResponse body] JSONObject]); 
}]; 

편집 : 당신은 또한주의해야한다 귀하의 브라우저에서 해당 URL을 작성하고 타격 경우 enter는 원하는대로 응답을 얻습니다. 그러면 POST 대신 GET을 사용하여 실제로 데이터를 보내야 할 수도 있습니다. 이 경우에는 위의 코드는 간단하게 :

[[UNIRest get:^(UNISimpleRequest *simpleRequest) { 
    [simpleRequest setUrl:@"http://10.222.0.100:8080/CRM/rest/user/login"]; 
    [simpleRequest setHeaders:@{@"accept": @"application/json"}]; 
    [simpleRequest setParameters:@{@"id": @"amit", @"password": @"amit123"}]; 
}] asJsonAsync:^(UNIHTTPJsonResponse *jsonResponse, NSError *error) { 
    NSLog(@"%@", [[jsonResponse body] JSONObject]); 
}]; 
1

내가이

-(void)tryThis 
{ 
NSString *[email protected]"{'login' : 'sdsd', 'password' : 'sdsd'}";  
a= [a stringByReplacingOccurrencesOfString:@"'" withString:@"\"" options:NSCaseInsensitiveSearch range:NSMakeRange (0, [a length])]; 

or 
NSString *[email protected]"{"login" : "sdsd", "password" : "sdsd"}"; 

NSData* postData= [a dataUsingEncoding:NSUTF8StringEncoding]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https:***********your url"]]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:[NSString stringWithFormat:@"%d", postData.length] forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:postData]; 
NSURLResponse *response = NULL; 
NSError *requestError = NULL; 
NSData *responseData1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError]; 
NSString *responseString = [[NSString alloc] initWithData:responseData1 encoding:NSUTF8StringEncoding]; 
} 
근무 자바 나머지 서비스에 대한 동일한 사용이 시도