iphone - 在 iPhone 中发布 URL 时,连接 didReceiveData 调用了两次?

标签 iphone url posting

我是 iPhone 开发新手。我已经发布了带有用户名和密码的 URL。我可以在“connection didReceiveData”方法中打印数据。但是我看到“connection didReceiveData”方法被调用了两次。我不知道我哪里出错了。这是我的代码

 - (void)viewDidLoad {
[super viewDidLoad];

NSString *post = [NSString stringWithFormat:@"&domain=school.edu&userType=2&referrer=http://apps.school.edu/navigator/index.jsp&username=%@&password=%@",@"xxxxxxx",@"xxxxxx"];

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];

[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://secure.school.edu/login/process.do"]]];

[request setHTTPMethod:@"POST"];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

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

[request setHTTPBody:postData];

NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];

if(conn)
{
    NSLog(@"Connection Successful");

}
else
{
    NSLog(@"Connection could not be made");
}

    }

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

NSString *string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"the  data %@",string);
  }

整个 HTML 页面在控制台中打印了两次。所以请帮助我。谢谢。

最佳答案

您可能会收到分块的响应数据,这就是 NSURLConnection's documentation 的原因状态:

委托(delegate)应该连接所交付的每个数据对象的内容,以构建 URL 加载的完整数据。”

为此使用 NSMutableData 实例,并且仅在收到 -connectionDidFinishLoading: 消息后才处理完整数据。

关于iphone - 在 iPhone 中发布 URL 时,连接 didReceiveData 调用了两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2446095/

相关文章:

apache - 在 apache windows 上禁止访问包含冒号符号 ":"的 url

java - 在 Java 中接受文件路径和 URL

android - 使用 Facebook android SDK 发布到 Facebook 特定的好友列表

iphone - 字符串中代码的 Unicode 字符 (Obj-C)

iphone - UIToolbar UIBarButtonItem 对齐问题

html - 通过 HTTrack 从下载的 wbsite 中删除域 URL

php - 使用ajax将数组发布到PHP

wordpress - 如何在 Wordpress 中获取使用 ACF 创建的帖子?

iphone - 创建图 block "on the fly"

iphone - 如何制作一个采用格式化字符串+几个附加参数的宏,就像 NSLog 一样?