Cannot connect iOS using AFNetworking to asp.net webservices -
i trying connect ios asp.net webservices , getting error 500. webservices working if connected web using jquery.ajax not ios. there settings have set on server side allow connection? possibly security settings not allowing outside connections? how make sure webserice returning json. when try enter url directly browser webservices directory , can invoke webservice error. working website though. here code using ios side
- (void)viewdidload { [super viewdidload]; nsdictionary *params = [nsdictionary dictionarywithobjectsandkeys: [nsdictionary dictionarywithobjectsandkeys:@"user", @"username",@"test", @"password",nil],@"request",nil]; afhttpclient *client = [[afhttpclient alloc] initwithbaseurl: [nsurl urlwithstring:@"http://www.server.com"]]; //[client setdefaultheader:@"contenttype" value:@"application/json; charset=utf-8"]; client.parameterencoding = afjsonparameterencoding; nsmutableurlrequest *request = [client requestwithmethod:@"post" path:@"/mobile/user.asmx/login" parameters:params]; nslog(@"request %@",request); afjsonrequestoperation *operation = [afjsonrequestoperation jsonrequestoperationwithrequest:request success:^(nsurlrequest *request, nshttpurlresponse *response, id json) { nslog(@"response %@",response); nslog(@"json: %@", [json valueforkeypath:@"loginresult"]); nslog(@"code: %@", [[[json valueforkeypath:@"loginresult"] valueforkeypath:@"code"] stringvalue]); nslog(@"faultstring: %@", [[json valueforkeypath:@"loginresult"] valueforkeypath:@"faultstring"]); } failure:^(nsurlrequest *request, nshttpurlresponse *response, nserror *error, id json) { nslog(@"error opening connection %d %@",response.statuscode, error); nslog(@"request %@",request); }]; [operation start]; }
and code works on website
$('#signin').click(function (e) { e.preventdefault(); var user = $('#user').val(); var password = $('#password').val(); var holdval = "<ul>"; if (user == null || user == "") holdval += "<li>provide user name</li>"; if (password == null || password == "") holdval += "<li>provide password</li>"; holdval += "</ul>"; if (holdval != "<ul></ul>") { $('#msg').show(); $('#msg').html(holdval); return; } var temp = new user(user, password); $("#loader").show(); var jsontxt = json.stringify({ user: temp }); jquery.ajax({ type: "post", url: "/mobile/user.asmx/login", //http://www.server.com contenttype: "application/json; charset=utf-8", data: jsontxt, datatype: "json", success: onsuccess, error: onerror }); });
update
as cory suggested used wireshark , check out http traffic. turned out request request still not able working. issues website request passes json data username , afnetworking passes username , password
this website http://www.server.com/user.asmx/loginuser{%22user%22:%22username%22,%22password%22:%22pass%22}
this ios http://www.server.com/user.asmx/loginuser?password=pass&user=username
it seems me parameters not becoming json
here updated code using on ios
afhttpclient *httpclient = [[afhttpclient alloc] initwithbaseurl:[nsurl urlwithstring:@"http://www.server.com/user.asmx/"]]; [httpclient setparameterencoding:afjsonparameterencoding]; [httpclient setdefaultheader:@"contenttype" value:@"application/json; charset=utf-8"]; [httpclient getpath:@"loginuser" parameters:@{@"user":@"username",@"password":@"pass"} success:^(afhttprequestoperation *operation, id responseobject) { nslog(@"response: %@", [[nsstring alloc] initwithdata:responseobject encoding:nsutf8stringencoding]); } failure:^(afhttprequestoperation *operation, nserror *error) { nslog(@"error: %@", error); }];
any suggestions appreciated!
update 2 after adding in first code above afsjonrequestoperation
[client setauthorizationheaderwithusername:username password:user_password]; //setting authorization header [client requestwithmethod:@"post" path:@"login/" parameters:nil]; //parameter nil [request sethttpmethod:@"get"];
i not getting errors or responses after start operation if check wireshark getting 401 unauthorized , and html saying credentials invalid. sure username , password correct. not sure if step further before. please let me know think. again!
get
requests serialize parameters in query string, because according rfc 2616, get
requests shouldn't include entity body.
for particular case, can work using code above (creating post
request before), adding following line after:
[request sethttpmethod:@"get"];
Comments
Post a Comment