这是我试图使用post方法为java rest webservice命中的Url
http://10.222.0.100:8080/CRM/rest/user/login?id=amit&password=amit123 // demo
下面是我用来点击services
的代码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它会返回true
http://10.222.0.100:8080/CRM/rest/user/login?id=amit&password=amit123 // demo
试试这个我使用相同的java rest服务,它工作
-(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];
}
我建议使用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]);
}];