【问题标题】:NSHTTPURLRequest headerNS HTTP URL 请求标头
【发布时间】:2010-06-29 02:12:29
【问题描述】:

我想从网站获取一些信息。我已经得到了网站的 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);

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

非常感谢。

【问题讨论】:

  • 你可以做NSLog(@"The header fields are %@", responseAllHeaderFields);

标签: iphone http-status-codes


【解决方案1】:

您收到警告是因为 NSInteger 是 int 的包装器。所以你不需要指针*。将分配的左侧更改为 NSInteger responseStatusCode 并查看警告是否消失。

【讨论】:

    【解决方案2】:

    感谢您的回复。

    但是,有一些警告。

    以下是代码

    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 种方法是错误的并且不能工作。

    谢谢。

    【讨论】:

    • 您仍在制作responseStatusCodeNSInteger *。放弃指针转换*,这就是问题所在。
    猜你喜欢
    • 2017-10-07
    • 2015-07-13
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    • 2015-08-20
    • 2016-06-29
    相关资源
    最近更新 更多