gpt4 book ai didi

ios - NSURLConnection 获取请求

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:08:04 26 4
gpt4 key购买 nike

我遇到了 NSURLConnection,我以前使用过它,只是根据请求获取数据并解析它。然而,这次 Web 开发人员开发了 GET 和 POST 请求。

我想通过许多教程和堆栈问题并尝试获得所需的结果。正如我所见,有时会有这样的请求

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"URL"]
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:10];

[request setHTTPMethod: @"GET"];

NSError *requestError;
NSURLResponse *urlResponse = nil;


NSData *response1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];

还有一些我见过的。

我看起来很简单,但我无法找到任何 POST 和 GET 请求所需的内容。

我从我的网络开发者那里收到的数据是

SOAP 1.2


POST /DEMOService/DEMO.asmx HTTP/1.1
Host: projects.demosite.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

作为返回会有 GET 和 POST

以下是一个示例 HTTP GET 请求和响应。显示的占位符需要替换为实际值。

     GET /DEMOService/DEMO.asmx/VerifyLogin?username=string&password=string&AuthenticationKey=string HTTP/1.1


Host: projects.demosite.com

我很清楚 NSURLConnections 的代表,它们正在跟随..

#pragma mark NSURLConnection Delegate Methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// A response has been received, this is where we initialize the instance var you created
// so that we can append data to it in the didReceiveData method
// Furthermore, this method is called each time there is a redirect so reinitializing it
// also serves to clear it
_responseData = [[NSMutableData alloc] init];

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to the instance variable you declared
[_responseData appendData:data];
}

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse {
// Return nil to indicate not necessary to store a cached response for this connection
return nil;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable now

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// The request has failed for some reason!
// Check the error var
}

唯一让我卡住的地方是

如何在 GET 或 POST 请求中传递参数的地方编写请求。

谢谢

最佳答案

如果您的参数在 URL 本身中发送(例如,作为 URL 路径或查询字符串的一部分),那么您只需将它们包含在 NSURL 参数中。例如,您可能有以下内容:

NSString *urlString = [NSString stringWithFormat:@"https://hostname/DEMOService/DEMO.asmx/VerifyLogin?username=%@&password=%@&AuthenticationKey=%@",
username,
password,
authenticationKey];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:10];

其中 usernamepasswordauthenticationKey 是您在别处设置的局部变量。

来自服务器的响应存储在 -[NSURLConnection sendSynchronousRequest:returningResponse:error:] 返回的 NSData 实例中包含的数据中。

所以在您的示例中,您上面的响应将存储在 response1 变量中。您可以将其转换为字符串和/或根据需要对其进行解析。

关于ios - NSURLConnection 获取请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18808683/

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