gpt4 book ai didi

php - 将此 .php 转换为 ObjC 可运行的 NSURLSession POST 请求?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:46:29 24 4
gpt4 key购买 nike

基本上,我正在尝试登录此 website called Mistar .我可以使用 php 来完成:

<form action="https://mistar.oakland.k12.mi.us/novi/StudentPortal/Home/Login" method="post">
<input type=text name="Pin">
<input type=password name="Password">
<input type="submit" id="LoginButton">
</form>

因此 php(当您运行它时)可以正常工作。您使用 Pin (20005012) 和密码 (wildcats) 进行身份验证并返回一个页面,其中包含类似 {1, User Authenticated}

现在我要做的是从 iPhone 应用程序(所以 ObjC)登录网站,可能使用 NSURLSession 或其他东西。

这是我目前所拥有的,但它一直给我一个登录页面错误:

- (void)loginToMistar {

//Create POST request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

//Create and send request
NSURL *url = [NSURL URLWithString:@"https://mistar.oakland.k12.mi.us/novi/StudentPortal/Home/Login"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSString *postString = [NSString stringWithFormat:@"<input type=text name='Pin'> <input type=password name='Password'> <input type='submit' id='LoginButton'>"];
NSData * postBody = [postString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:postBody];

// [request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

NSOperationQueue *queue = [[NSOperationQueue alloc] init];

NSString* url=[NSString stringWithFormat:url];

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
// do whatever with the data...and errors
if ([data length] > 0 && error == nil) {
NSString *loggedInPage = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(loggedInPage);
}
else {
NSLog(@"error");
}
}];

谁能告诉我代码中的问题出在哪里?

最佳答案

  1. 通常,您需要指定请求的Content-type (application/x-www-form-urlencoded)。

  2. 您想要构建请求的主体以符合该类型的请求(如 cmorrissey 所说),例如Pin=xxx&Password=yyy

  3. 您要确保对这些值进行百分比转义,xxxyyy(因为,如果密码包含任何保留字符,例如 + ,密码将无法正确传输)。

  4. 您需要指定您的请求是一个POST 请求。因此,请使用原始的 NSMutableURLRequest 并正确配置它并停用您的 NSURLRequest

  5. 你不想要这样的行:

    NSString* url=[NSString stringWithFormat:url];
  6. 您不必创建操作队列。你可以,但是(a)你并没有做一些非常缓慢且计算量大的事情; (b) 很可能这个完成 block 最终可能想要更新 UI,而你永远不会在后台队列上这样做,而只能在主队列上这样做。

  7. 由于响应似乎是 JSON,让我们继续解析该响应(如果可以的话)。

因此,您最终会得到如下结果:

- (void)loginToMistarWithPin:(NSString *)pin password:(NSString *)password {

NSURL *url = [NSURL URLWithString:@"https://mistar.oakland.k12.mi.us/novi/StudentPortal/Home/Login"];

//Create and send request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];

NSString *postString = [NSString stringWithFormat:@"Pin=%@&Password=%@",
[self percentEscapeString:pin],
[self percentEscapeString:password]];
NSData * postBody = [postString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:postBody];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
// do whatever with the data...and errors
if ([data length] > 0 && error == nil) {
NSError *parseError;
NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
if (responseJSON) {
// the response was JSON and we successfully decoded it

NSLog(@"Response was = %@", responseJSON);
} else {
// the response was not JSON, so let's see what it was so we can diagnose the issue

NSString *loggedInPage = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Response was not JSON, it was = %@", loggedInPage);
}
}
else {
NSLog(@"error: %@", error);
}
}];
}

- (NSString *)percentEscapeString:(NSString *)string
{
NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)string,
(CFStringRef)@" ",
(CFStringRef)@":/?@!$&'()*+,;=",
kCFStringEncodingUTF8));
return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"];
}

然后你可以这样调用它:

[self loginToMistarWithPin:@"20005012" password:@"wildcats"];

关于php - 将此 .php 转换为 ObjC 可运行的 NSURLSession POST 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22567214/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com