问题描述
我正在接近iOS开发,我想让我的第一个应用程序之一执行HTTP POST请求。
I'm approaching iOS development and I'd like to have one of my first applications to perform a HTTP POST request.
据我所知,我应该管理通过 NSURLConnection
对象处理请求的连接,这会强制我有一个委托对象,而该对象又会处理数据事件。
As far as I can understand, I should manage the connection which handles the request via a NSURLConnection
object, which forces me to have a delegate object, which in turn will handle data events.
有人可以通过一个实际例子澄清任务吗?
Could someone please clarify the task with a practical example?
我应该联系https端点发送身份验证数据(用户名和密码)和获取纯文本回复。
I should contact an https endpoint sending authentication data (username and password) and getting back a plain text response.
推荐答案
您可以按如下方式使用NSURLConnection:
You can use NSURLConnection as follows:
-
设置
NSURLRequest
:使用requestWithURL:(NSURL *)theURL
初始化请求。
Set your
NSURLRequest
: UserequestWithURL:(NSURL *)theURL
to initialise the request.
如果您需要指定POST请求和/或HTTP标头,请使用 NSMutableURLRequest
with
If you need to specify a POST request and/or HTTP headers, use NSMutableURLRequest
with
-
(void)setHTTPMet hod:(NSString *)方法
-
(void)setHTTPBody:(NSData *)data
-
(void)setValue:(NSString *)值为HTTPHeaderField:(NSString *)字段
(void)setHTTPMethod:(NSString *)method
(void)setHTTPBody:(NSData *)data
(void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field
使用 NSURLConnection
发送您的请求:
Send your request in 2 ways using NSURLConnection
:
-
同步:
(NSData *)sendSynchronousRequest:(NSURLRequest *)请求returningResponse:(NSURLResponse **)响应错误:(NSError) **)错误
这将返回一个可以处理的 NSData
变量。
This returns a NSData
variable that you can process.
重要提示:请记住在单独的线程中启动同步请求以避免阻止UI。
IMPORTANT: Remember to kick off the synchronous request in a separate thread to avoid blocking the UI.
异步:(无效)开始
不要忘记设置NSURLConnection的委托来处理连接,如下所示:
Don't forget to set your NSURLConnection's delegate to handle the connection as follows:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[self.data setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d {
[self.data appendData:d];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", @"")
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", @"")
otherButtonTitles:nil] autorelease] show];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *responseText = [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding];
// Do anything you want with it
[responseText release];
}
// Handle basic authentication challenge if needed
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSString *username = @"username";
NSString *password = @"password";
NSURLCredential *credential = [NSURLCredential credentialWithUser:username
password:password
persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
}
这篇关于iOS:如何执行HTTP POST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!