2

我遇到了 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 请求中编写传递参数的请求。

谢谢

4

1 回答 1

7

如果您的参数是在 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是您在其他地方设置的局部变量。

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

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

于 2013-09-15T03:42:13.580 回答