我正在尝试从 iOS 连接到 asp.net webservices,但我收到错误 500。如果使用 jQuery.ajax 从 Web 连接而不是从 iOS 连接,则 Web 服务正在工作。是否必须在服务器端设置任何设置才能允许连接?可能是一些不允许外部连接的安全设置?我如何确保网络服务返回 json。当我尝试将 url 直接输入浏览器时,我得到了 webservices 目录,我可以调用 webservice,但出现错误。不过,一切都在网站上完美运行。这是我在 iOS 端使用的代码
- (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];
}
这是在网站上运行的代码
$('#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
});
});
更新
正如 Cory 建议的那样,我使用了 wireshark 并检查了 http 流量。原来该请求是一个 GET 请求,但我仍然无法使其正常工作。问题是网站请求使用用户名传递 json 数据,而 afnetworking 仅传递用户名和密码
这是来自网站 http://www.server.com/user.asmx/loginuser {%22user%22:%22username%22,%22password%22:%22pass%22}
这是来自 iOS http://www.server.com/user.asmx/loginuser?password=pass&user=username
在我看来,参数没有变成 json
这是我在 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);
}];
任何建议将不胜感激!
更新 2 使用 afsjonrequestoperation 在上面的第一个代码中添加此内容后
[client setAuthorizationHeaderWithUsername:USERNAME password:USER_PASSWORD]; //setting authorization header
[client requestWithMethod:@"POST" path:@"login/" parameters:nil]; //parameter nil
[request setHTTPMethod:@"GET"];
开始操作后,我没有收到任何错误或响应,但如果我检查 wireshark,我会收到 401 Unauthorized 和 html 说凭据无效。我确定用户名和密码是正确的。我不确定这是否比我以前更进一步。请让我知道你的想法。再次感谢!