阅读NSURLresponse [英] Read NSURLresponse

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

问题描述

我向此发送对象: https://sandbox.itunes.apple.com/verifyReceipt

NSUrlconnection
,我想用这个委托方法读取:

with NSUrlconnection and i am trying to read it with this delegate method :

code> - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
这样:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response like this :

NSlog(@%@,response);
i得到此代码:

NSlog(@"%@",response); i am getting this code :

< NSHTTPURLResponse:0x7d2c6c0>
i需要得到一个字符串。
我如何阅读它?

<NSHTTPURLResponse: 0x7d2c6c0> i need to get a string somehow. how can i read it?

推荐答案

我写这个答案另一个问题,但我认为它会帮助你。特别是看看方法

I wrote this answer to another question, but I think it will help you. Have a look in particular at the methods

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

- (void)connectionDidFinishLoading:(NSURLConnection * )connection

-(void) requestPage
{
    NSString *urlString = @"http://the.page.you.want.com";
    NSURL *url = [NSURL URLWithString:urlString];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20.0f];


    responseData = [[NSMutableData alloc] init];
    connection = [[NSURLConnection connectionWithRequest:request delegate:self] retain];
    delegate = target;
}


-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{   
    if ([response isKindOfClass:[NSHTTPURLResponse class]])
    {
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response; 
        //If you need the response, you can use it here
    }
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [responseData appendData:data];
}

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [responseData release];
    [connection release];
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
    if (connection == adCheckConnection)
    {
        NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

        //You've got all the data now
        //Do something with your response string


        [responseString release];
    }

    [responseData release];
    [connection release];
}

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

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