阅读NSURLresponse
我发送一个对象到这个意见: https://sandbox.itunes.apple.com/verifyReceipt
://sandbox.itunes.apple.com/verifyReceipt
与NSUrlconnection
,我想用这个委托方法读取它:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
如下:
NSlog(@"%@",response);
我得到这个代码:
<NSHTTPURLResponse: 0x7d2c6c0>
我需要得到一个string莫名其妙。 我怎么读?
我写了这个答案的另一个问题,但我认为这会帮助你。 请特别注意方法
-(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]; }
NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response; int errorCode = httpResponse.statusCode; NSString *fileMIMEType = [[httpResponse MIMEType] lowercaseString];
有关更多信息,请检查iOS文档: NSHTTPURLResponse
。
并要耐心:不是所有的连接都返回NSHTTPURLResponse
如果您希望连接正在接收您可以使用的一些数据。
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
你可以简单地将数据转换为NSString
。
您可以创build一个子类并覆盖- (NSString*) description
方法。