gpt4 book ai didi

iPhone 认证流程

转载 作者:行者123 更新时间:2023-11-29 04:46:10 25 4
gpt4 key购买 nike

我无法找到有关此主题的信息。请在这里帮助我。

我需要通过 POST 或 GET 方法将参数传递到我的网络服务器并获得回复。

基本上,如果使用 GET 方法,我想做一些类似 server.com/?user=john&password=smith 的事情,并接收使用我的 php< 完成的动态生成的 HTML 代码/strong> 脚本。所有这一切都无需在我的应用程序上使用网络浏览器。

通常是如何完成的?

最佳答案

您需要查看 NSMutableURLRequestNSURLConnection

例如,您可以像这样使用它们来向服务器发出 GET 请求:

- (void)loginUser:(NSString *)username withPassword:(NSString *)password {

// GET
NSString *serverURL = [NSString stringWithFormat:@"http://yourserver.com/login.php?user=%@&pass=%@", username, password];

NSURL *url = [NSURL URLWithString:serverURL];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];

NSURLConnection *connection = [NSURLConnection connectionWithRequest:req delegate:self];

if (connection) {
connectionData = [[NSMutableData alloc] init];
}
}

这将向您的服务器发送一个异步 GET 请求,其中的查询字符串包含用户名和密码。

如果您想使用 POST 请求发送用户名和密码,该方法将如下所示:

- (void)loginUser:(NSString *)username withPassword:(NSString *)password {

// POST
NSString *myRequestString = [NSString stringWithFormat:@"user=%@&pass=%@",username,password];
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];

NSURL *url = [NSURL URLWithString:@"http://yourserver.com/login.php"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod: @"POST"];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[req setHTTPBody: myRequestData];

NSURLConnection *connection = [NSURLConnection connectionWithRequest:req delegate:self];

if (connection) {
connectionData = [[NSMutableData alloc] init];
}
}

为了从服务器获取响应,您需要实现 NSURLConnection 委托(delegate)方法,例如:

#pragma mark -
#pragma mark NSURLConnection delegate methods
#pragma mark -

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {

// Called if you have an .htaccess auth. on server
NSURLCredential *newCredential;
newCredential = [NSURLCredential credentialWithUser:@"your_username" password:@"your_password" persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
}

-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

[connectionData setLength: 0];
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

[connectionData appendData:data];
}

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

[connectionData release];
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection {


NSString *content = [[NSString alloc] initWithBytes:[connectionData bytes]
length:[connectionData length] encoding: NSUTF8StringEncoding];

// This will be your server's HTML response
NSLog(@"response: %@",content);

[content release];
[connectionData release];
}

引用文献:

NSMutableURLRequest Class Reference

NSURLConnection Class Reference

希望这有帮助:)

关于iPhone 认证流程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9616620/

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