1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- //
- // ZGWebUtils.m
- // UUTUtils
- //
- // Created by zhuge on 2017/12/26.
- // Copyright © 2017年 zhuge. All rights reserved.
- //
- #import "ZGWebUtils.h"
- #define kWebTimeout 30
- @implementation ZGWebUtils
- + (void)getForUrl:(NSString *)strUrl completionHandler:(void (^)(NSData * _Nullable, NSURLResponse * _Nullable, NSError * _Nullable))completionHandler {
- NSURL *url = [NSURL URLWithString:strUrl];
- NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:kWebTimeout];
- NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:completionHandler];
- [dataTask resume];
- }
- + (NSString *)get:(NSString *)strUrl {
- NSURL *url = [NSURL URLWithString:strUrl];
- NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:kWebTimeout];
- NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
- NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- NSLog(@"%@", result);
- }];
- [dataTask resume];
- return @"";
- // NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request];
- // dataTask.state
- }
- +(NSString *)stringOfURL:(NSString *)url {
- NSURL *urlRequest = [NSURL URLWithString:url];
- NSURLRequest *request = [NSURLRequest requestWithURL:urlRequest cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:kWebTimeout];
- NSError *error = nil;
- NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
- if (error) {
- NSLog(@"≈Error:%@", [error localizedDescription]);
- return [error localizedDescription];
- }
- return [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
- }
- //+(NSString *)stringOfURLByPost:(NSString *)url postData:(NSData*)postData {
- // NSURL *urlRequest = [NSURL URLWithString:url];
- // NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:urlRequest cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
- // [request setHTTPMethod:@"POST"];//设置请求方式为POST,默认为GET
- // [request setHTTPBody:postData];
- // NSError *error = nil;
- // NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
- // if (error) {
- // NSLog(@"≈Error:%@", [error localizedDescription]);
- // return [error localizedDescription];
- // }
- // return [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
- //}
- //
- //+ (NSDictionary *)jsonDicOfURL:(NSString *)url {
- // NSString *str = [ZGWebUtils stringOfURL:url];
- // return [ZGJsonUtils stringToObject:str];
- //}
- //
- //+ (NSArray *)jsonArrOfURL:(NSString *)url {
- // NSString *str = [ZGWebUtils stringOfURL:url];
- // return [ZGJsonUtils stringToObject:str];
- //}
- //
- //+(NSString*) stringOfURLWithDic:(NSString*)urlPrefix :(NSDictionary*)dic{
- // NSString *json = [ZGJsonUtils dictionaryToString:dic];
- // NSString *url = [NSString stringWithFormat:@"%@%@", urlPrefix, [ZGStringUtils urlEncode:json]];
- //
- // return [ZGWebUtils stringOfURL:url];
- //}
- +(NSString *)stringOfURLByPost:(NSString *)url postData:(NSData*)postData {
- NSURL *urlRequest = [NSURL URLWithString:url];
- NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:urlRequest cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
- [request setHTTPMethod:@"POST"];//设置请求方式为POST,默认为GET
- [request setHTTPBody:postData];
- NSError *error = nil;
- NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
- if (error) {
- NSLog(@"≈Error:%@", [error localizedDescription]);
- return [error localizedDescription];
- }
- return [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
- }
- @end
|