你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> https 寫了一個數據返回類

https 寫了一個數據返回類

編輯:IOS開發綜合
// //  httpsClass.h //  https // //  Created by 夏 科傑 on 12-12-20. //  Copyright (c) 2012年 夏 科傑. All rights reserved. //   #import <Foundation/Foundation.h>   @interface httpsClass : NSObject {     NSMutableURLRequest* _request;     NSURLConnection *  connection;     NSStringEncoding enc;     NSURLAuthenticationChallenge *_challenge;     NSMutableData* muData;     bool isFinish; } -(NSString *)initWithUrl:(NSString* )url postData:(NSString* )data; @end     // //  httpsClass.m //  https // //  Created by 夏 科傑 on 12-12-20. //  Copyright (c) 2012年 夏 科傑. All rights reserved. //   #import "httpsClass.h"   @implementation httpsClass       -(NSString *)initWithUrl:(NSString* )requestUrl postData:(NSString* )data {        enc=CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);     _request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:requestUrl]];     NSData *aData = [data dataUsingEncoding: NSUTF8StringEncoding];     [_request setHTTPMethod: @"POST"];     [_request setHTTPBody:aData];     assert(_request != nil);     connection = [NSURLConnection connectionWithRequest:_request delegate:self];     [self _receiveDidStart];          if(connection)     {         muData = [[NSMutableData data] retain];         NSLog(@"intial done!");     }     else              {                  NSLog(@"sorry");              }     isFinish=NO;     while(!isFinish) {              [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];     }     NSLog(@"%@",muData);          return [[NSString alloc] initWithData:muData  encoding:NSUTF8StringEncoding]; }     #pragma mark - Tell the UI we are receiving or received. - (void)_receiveDidStart {     NSLog(@"receiving"); }   - (void)_receiveDidStopWithStatus:(NSString *)statusString {     if (statusString == nil) {         NSLog(@"Get succeeded");     }else         NSLog(@"Get not succeeded");         // NSLog(@"---%@",statusString); } - (void)_stopReceiveWithStatus:(NSString *)statusString {     if (connection != nil) {         [connection cancel];         connection = nil;     }     if (_challenge !=nil) {         [_challenge release];     }     [self _receiveDidStopWithStatus:statusString]; }     - (BOOL)connection:(NSURLConnection *)conn canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {     NSLog(@"authenticate method:%@",protectionSpace.authenticationMethod);     return [protectionSpace.authenticationMethod isEqualToString:             NSURLAuthenticationMethodServerTrust]; }   - (void)connection:(NSURLConnection *)conn didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {     NSLog(@"%@",challenge);     _challenge=[challenge retain];     NSURLCredential *   credential;          NSURLProtectionSpace *  protectionSpace;     SecTrustRef             trust;     NSString *              host;     SecCertificateRef       serverCert;   //三次握手     assert(_challenge !=nil);     protectionSpace = [_challenge protectionSpace];     assert(protectionSpace != nil);     trust = [protectionSpace serverTrust];     assert(trust != NULL);     credential = [NSURLCredential credentialForTrust:trust];     assert(credential != nil);     host = [[_challenge protectionSpace] host];     if (SecTrustGetCertificateCount(trust) > 0) {         serverCert = SecTrustGetCertificateAtIndex(trust, 0);     } else {         serverCert = NULL;     }     [[_challenge sender] useCredential:credential forAuthenticationChallenge:_challenge];      }   - (void)connection:(NSURLConnection *)conn didReceiveResponse:(NSURLResponse *)response {              NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse *) response;     assert( [httpResponse isKindOfClass:[NSHTTPURLResponse class]] );     if ((httpResponse.statusCode / 100) != 2) {         [self _stopReceiveWithStatus:[NSString stringWithFormat:@"HTTP error %zd", (ssize_t) httpResponse.statusCode]];     } else {                  NSLog(@"status: ok");     }     //NSLog(@"%@",httpResponse);     NSLog(@"get the whole response");     [muData setLength:0]; }     - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data   {          NSLog(@"get some data");          [muData appendData:data];//返回數據      }     - (void)connection:(NSURLConnection *)conn didFailWithError:(NSError *)error {     NSLog(@"didFailWithError %@", error);          [self _stopReceiveWithStatus:@"Connection failed"]; }   - (void)connectionDidFinishLoading:(NSURLConnection *)conn { #pragma unused(conn)          NSLog(@"connectionDidFinishLoading");     isFinish=YES;     [self _stopReceiveWithStatus:nil]; }       @end       調用方法          http=[httpsClass new];        NSString *postPram = [NSString stringWithFormat:@"requestData={\"api_Channel\":\"3\",\"api_name\":\"api.news.find_article_weekly_daily_news_list\",\"params\":{\"type\":%d,\"pageNo\":%d,\"pageSize\":%d}}",1,1,10];        NSLog(@"%@",[http initWithUrl:@"https://**.**.**.**/api/x.htm" postData:postPram]);   就會打印你需要的返回值。
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved