如何为post java rest服务授权用户名和密码

时间:2014-03-27 11:00:46

标签: ios objective-c

以下是我尝试使用jave rest webservice的post方法点击的Url

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

以下是我用来点击服务的代码

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];
         }
     }
 }];
}

但它总是会失败,任何人都可以让我知道代码的错误。

如果我在浏览器中点击整个网址,它就会恢复正常。

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

2 个答案:

答案 0 :(得分:1)

我建议使用UNIRest与Objective-C中的RESTful API进行交互。你可以在这里得到它: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键可以获得您所希望的响应,那么您实际上可能需要使用GET而不是POST来发送数据。在这种情况下,上面的代码就变成了:

[[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 :(得分:1)

试试这个我使用相同的java休息服务吗?

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

or
NSString *a=@"{"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];   
 }