当前位置:编程学习 > wap >>

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");
    }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"];
补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,