Reputation: 3616
I am trying to connect from iOS to asp.net webservices and i am getting error 500. The webservices are working if connected from the web using jQuery.ajax but not from iOS. Are there any settings that have to be set on the server side to allow the connection? Possibly some security settings that are not allowing outside connections? How do i make sure that the webserice is returning json. When i try to enter the url directly into the browser i get the webservices directory and i can invoke the webservice but i get an error. Everything is working perfectly from the website though. Here is the code that i am using from the 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 this is the code that works on the 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 i used wireshark and check out the http traffic. Turned out the request is a GET request but i am still not able to get this working. The issues is the website request passes json data with username and afnetworking passes just username and password
This is from the website http://www.server.com/user.asmx/loginuser{%22user%22:%22username%22,%22password%22:%22pass%22}
This is from iOS http://www.server.com/user.asmx/loginuser?password=pass&user=username
It seems to me that the parameters are not becoming json
Here is the updated code that i am 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 would be greatly appreciated!
UPDATE 2 After adding this in the first code above with afsjonrequestoperation
[client setAuthorizationHeaderWithUsername:USERNAME password:USER_PASSWORD]; //setting authorization header
[client requestWithMethod:@"POST" path:@"login/" parameters:nil]; //parameter nil
[request setHTTPMethod:@"GET"];
I am not getting any errors or responses after i start the operation but if i check wireshark i am getting 401 Unauthorized and and html saying that credentials are invalid. I am sure that username and password are correct. I am not sure if this is step further then i was before. Please let me know what you think. Thanks again!
Upvotes: 0
Views: 1382
Reputation: 19544
GET
requests will serialize parameters in the query string, because according to RFC 2616, GET
requests shouldn't include an entity body.
For your particular case, you can get this to work by using the code above (creating the POST
request as before), but adding the following line immediately after:
[request setHTTPMethod:@"GET"];
Upvotes: 1