NSHTTPURLRequest标头 [英] NSHTTPURLRequest header

查看:96
本文介绍了NSHTTPURLRequest标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to get some information from a website. I have already got the HTML code of the website. However, I would like to get more header information of the website (e.g. http status code)

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

After read the apple library, I found that there are 2 methods which can display those information. However, I can only use one of the methods (has warning), another cannot be used.

以下是代码.

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);

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

Besides, are there are any other methods to get the header informatino?

非常感谢您.

推荐答案

感谢您的答复.

但是,有一些警告.

以下是代码

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种方法是错误的并且无法使用.

I have no ideas why the above 3 methods are wrong and cannot be work.

谢谢.

这篇关于NSHTTPURLRequest标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆