https 写了一个数据返回类
//// httpsClass.h// https//// Created by 夏 科杰 on 12-12-20.// Copyright (c) 2012年 夏 科杰. All rights reserved.//#import <Foundation/Foundation.h>@inte易做图ce 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");}elseNSLog(@"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"];补充:移动开发 , IOS ,
上一个:IOS文件操作
下一个:ios开发答疑录系列​---ios编译正常,但无法运行到真机和模拟器上 ,Choose a destination with a supported architecture
- 更多wap疑问解答:
- 新人求助QPainter
- 为什么程序都退出了还可以收到推送?如果大多设备都可以推送那运营商怎么办?
- qt 4.7 sqlserver2000 存储过程调用
- 关于ANDROID4.0.1编译问题!
- Android FrameBuffer读屏幕30秒后mmap失败
- 联通粗定位用java程序如何来请求和接受数据
- 为什么QT运行Android平台的程序时,mouseMoveEvent事件响应的间隔时间很长??????????
- android与PC蓝牙通讯
- 指定大小的label 内容可变,如果内容超出label的宽度,将未能显示的部分显示在另一个label上
- Android调试
- android如何通过wifi连接无线打印机
- 运行程序,release目录下产生一个乱码文件夹
- 分享个某机构最新安卓资料,自己验证了
- service启动不起来,掉不了service connection
- 求助:QT5.0 没有QPrinter吗