0

我想从网站上获取一些信息。我已经得到了网站的 HTML 代码。但是,我想获取更多网站的标头信息(例如 http 状态码)

阅读苹果库后,我发现有两种方法可以显示这些信息。但是,我只能使用其中一种方法(有警告),另一种方法不能使用。

以下是代码。

NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString *data=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];

NSDictionary *responseAlllHeaderFields = [response allHeaderFields];
// how can I convent the NSDictionary to the NSString, I cannot use the NSLog to display the information
NSLog(responseAlllHeaderFields); // <-- wrong

// returns the HTTP status code for the response
NSInteger *responseStatusCode = [response statusCode];
// warning: initialization makes pointer from integer without a cast
NSLog(@"\nThe status code is %d\n", responseStatusCode);

此外,还有其他方法可以获取标头信息吗?

非常感谢你。

4

2 回答 2

3

您收到警告是因为 NSInteger 是int. 所以你不需要指针*。将作业的左侧更改为 justNSInteger responseStatusCode并查看警告是否消失。

于 2010-06-29T02:15:39.330 回答
1

感谢你的回复。

但是,有一些警告。

以下是代码

NSInteger *responseStatusCode = [[response statusCode] intValue];
NSLog(@"\nThe status code is %d\n", responseStatusCode);

NSInteger *responseStatusCode = [response statusCode];
[responseStatusCode intValue];
NSLog(@"\nThe status code is %d\n", responseStatusCode);

NSInteger *responseStatusCode = [response statusCode];
NSLog(@"\nThe status code is %d\n", [responseStatusCode intValue]);

我不知道为什么上述 3 种方法是错误的并且不能工作。

谢谢你。

于 2010-06-29T02:23:03.697 回答