读书人

https 写了一个数据回到类

发布时间: 2012-12-31 11:57:52 作者: rapoo

https 写了一个数据返回类

//

// 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 = [NSMutableURLRequestrequestWithURL:[NSURLURLWithString:requestUrl]];

NSData *aData = [datadataUsingEncoding: NSUTF8StringEncoding];

[_requestsetHTTPMethod: @"POST"];

[_requestsetHTTPBody:aData];

assert(_request !=nil);

connection = [NSURLConnectionconnectionWithRequest:_requestdelegate:self];

[self_receiveDidStart];

if(connection)

{

muData = [[NSMutableDatadata] retain];

NSLog(@"intial done!");

}

else

{

NSLog(@"sorry");

}

isFinish=NO;

while(!isFinish) {


[[NSRunLoopcurrentRunLoop] runMode:NSDefaultRunLoopModebeforeDate:[NSDatedistantFuture]];

}

NSLog(@"%@",muData);

return [[NSStringalloc] 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) {

[connectioncancel];

connection = nil;

}

if (_challenge !=nil) {

[_challengerelease];

}

[self_receiveDidStopWithStatus:statusString];

}



- (BOOL)connection:(NSURLConnection *)conn canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace

{

NSLog(@"authenticate method:%@",protectionSpace.authenticationMethod);

return [protectionSpace.authenticationMethodisEqualToString:

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 = [_challengeprotectionSpace];

assert(protectionSpace != nil);

trust = [protectionSpaceserverTrust];

assert(trust != NULL);

credential = [NSURLCredentialcredentialForTrust:trust];

assert(credential != nil);

host = [[_challengeprotectionSpace] host];

if (SecTrustGetCertificateCount(trust) >0) {

serverCert =SecTrustGetCertificateAtIndex(trust, 0);

}else {

serverCert =NULL;

}

[[_challengesender] useCredential:credentialforAuthenticationChallenge:_challenge];

}


- (void)connection:(NSURLConnection *)conn didReceiveResponse:(NSURLResponse *)response

{

NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse *) response;

assert( [httpResponse isKindOfClass:[NSHTTPURLResponse class]] );

if ((httpResponse.statusCode /100) != 2) {

[self_stopReceiveWithStatus:[NSStringstringWithFormat:@"HTTP error %zd", (ssize_t) httpResponse.statusCode]];

}else {

NSLog(@"status: ok");

}

//NSLog(@"%@",httpResponse);

NSLog(@"get the whole response");

[muDatasetLength:0];

}



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


{

NSLog(@"get some data");

[muDataappendData: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=[httpsClassnew];

NSString *postPram = [NSStringstringWithFormat:@"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(@"%@",[httpinitWithUrl:@"https://**.**.**.**/api/x.htm"postData:postPram]);


就会打印你需要的返回值。
1楼x1135768777前天 13:00
忘了说要加两个frameworknSecurity.frameworknCFNetwork.framework

读书人网 >移动开发

热点推荐