ios 读取 NSURL 响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8036895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:15:58  来源:igfitidea点击:

Read NSURLresponse

iosobjective-cxcodecocoa-touchnsurlconnection

提问by or azran

i am sending an object to this adrees : https://sandbox.itunes.apple.com/verifyReceipt

我正在向这个 adrees 发送一个对象: https://sandbox.itunes.apple.com/verifyReceipt

with NSUrlconnectionand i am trying to read it with this delegate method :

NSUrlconnection,我试图用这个委托方法来阅读:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)responselike this :

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

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

NSlog(@"%@",response);我得到这个代码:

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

<NSHTTPURLResponse: 0x7d2c6c0>我需要以某种方式得到一个字符串。我怎么读?

回答by James Webster

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) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

and

-(void) connectionDidFinishLoading:(NSURLConnection *)connection

-(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];
}

回答by Nekto

NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response;
int errorCode = httpResponse.statusCode;
NSString *fileMIMEType = [[httpResponse MIMEType] lowercaseString];

For more information, check iOS Docs : NSHTTPURLResponse.

有关更多信息,请查看 iOS 文档:NSHTTPURLResponse

And be patient: not all connections return NSHTTPURLResponse

并耐心等待:并非所有连接都返回 NSHTTPURLResponse

回答by Himanshu A Jadav

If you expect that the connection is receiving some data you can use.

如果您希望连接正在接收一些您可以使用的数据。

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

you can then simply convert data to NSString.

然后,您可以简单地将数据转换为NSString.

回答by SAKrisT

You can create a subclass and override the - (NSString*) descriptionmethod.

您可以创建一个子类并覆盖该- (NSString*) description方法。