码迷,mamicode.com
首页 > 其他好文 > 详细

同步发送请求模块

时间:2015-07-24 18:17:20      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:同步发送请求   sendsynchr   requestwit   

同步加载有两种,一个是直接加载方式,另一个是发送标准同步请求后生成页面的方式。
直接方式的实现是requestWithURL:url,这种方案基本上已经很少有人用了。
发送标准同步请求的方式就是通过sendSynchronousRequest这个函数实现。
咱们的这个标准同步请求模块是通过代理来实现,就只贴代码,步贴使用方法了。
chttpSendSynchronousRequest.h文件

#import <Foundation/Foundation.h>

@interface chttpSendSynchronousRequest : NSObject

- (void)sendHttpRequest:(NSString*)sendString : (NSInteger *)resultCode : (NSString **)recvData;
@end

chttpSendSynchronousRequest.m文件

- (void)sendHttpRequest:(NSString*)sendString : (NSInteger *)resultCode : (NSString **)recvData
{

    if(nil == serverUrl) //|| (nil == sendString))
    {
        *resultCode = -1;
        recvData = nil;        
        return;
    }

    NSURL *serverNUSUrl = [NSURL URLWithString:serverUrl];

    NSLog(@"%@", sendString);

    NSError *error = nil;
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:serverNUSUrl
                                                           cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];

    [request setHTTPMethod:@"POST"];

    [request setValue:@"text.html" forHTTPHeaderField:@"Content-Type"];
    NSString * sendData = sendString;
    NSData *postData = [sendData dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];

    NSInputStream *is = [NSInputStream inputStreamWithData:postData];
    [request setHTTPBodyStream:is];


    NSData *nsRecvData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];

    if(nil != error)
    {
        *resultCode = -1;
        recvData = nil;
        return;
    }

    NSString * recvStrData = [[NSString alloc] initWithData:nsRecvData encoding:NSUTF8StringEncoding];

    if(nil == recvStrData)
    {
        *resultCode = -1;
        recvData = nil;
        return;
    }
    NSLog(@"recvStrData[%@]",recvStrData);
    *recvData = recvStrData;
    *resultCode = 0;
    return;
}
@end

通过requestWithURL:url实现直接同步加载图像代码示例:

- (void)loadImageFromURL:(NSURL*)url {
    NSURLRequest* request = [NSURLRequest requestWithURL:url
                                             cachePolicy:NSURLRequestUseProtocolCachePolicy
                                         timeoutInterval:10.0];
    [_data setLength:0];
    _connection = [[NSURLConnection alloc]
                  initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)theConnection
    didReceiveData:(NSData *)incrementalData {
    NSData * da = [[NSData alloc] initWithData:incrementalData];
    [_data appendData:da];

    LogInfo(@"succeeded  %d byte received", [_data length]);
    NSString* fileName2 = [self filePath:@"image23.jpg"];
    [da writeToFile:fileName2 atomically:YES];
}


- (void)connection:(NSURLConnection *)theConnection didFailWithError:(NSError *)error{
    // 当请求失败时的相关操作;
    [_data setLength:0];
}

- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
    LogInfo(@"succeeded  %d byte received", [_data length]);
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

同步发送请求模块

标签:同步发送请求   sendsynchr   requestwit   

原文地址:http://blog.csdn.net/jia12216/article/details/47044357

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!