AllPayRequest.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // UPRequest.m
  3. // VTPayUPDemo
  4. //
  5. // Created by 司瑞华 on 15/7/29.
  6. // Copyright (c) 2015年 __VTPayment__. All rights reserved.
  7. //
  8. #import "AllPayRequest.h"
  9. #import "commonCrypto/CommonDigest.h"
  10. @implementation AllPayRequest
  11. ///**
  12. // * post请求
  13. // *
  14. // * @param urlStr 要访问的网站 NSString类型
  15. // * @param paramters 请求的参数 NSMutableDictionary
  16. // * @param succeedBlock 成功时的回调 返回NSString类型
  17. // * @param failedBlock 失败时的回调 返回NSString类型
  18. // */
  19. +(void)postRequestWithURL:(NSString *)urlStr
  20. paramters:(NSMutableDictionary *)paramters
  21. succeed:(FinishBlock)succeedBlock
  22. failed:(FailedBlock)failedBlock
  23. {
  24. AllPayRequest * httpRequest = [[AllPayRequest alloc]init];
  25. httpRequest.finishBlock = succeedBlock;
  26. httpRequest.failedBlock = failedBlock;
  27. NSURL * url = [NSURL URLWithString:urlStr];
  28. NSMutableURLRequest * request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
  29. NSString * strField;
  30. if (paramters.count>0)
  31. {
  32. strField = [self dictFieldInfo:paramters];
  33. }else
  34. {
  35. strField = @"";
  36. }
  37. ///把获得的整个请求字段用utf-8编码一下
  38. NSData * data = [strField dataUsingEncoding:NSUTF8StringEncoding];
  39. [request setHTTPBody:data];
  40. [request setHTTPMethod:@"POST"];
  41. NSURLConnection * connection = [[NSURLConnection alloc]initWithRequest:request delegate:httpRequest];
  42. [connection start];
  43. }
  44. // 将字典参数 拼接成一个有 &%@=%@ 的字符串
  45. +(NSString *)dictFieldInfo:(NSMutableDictionary*)dict
  46. {
  47. NSMutableString * Url = [NSMutableString string];
  48. NSArray * keys = [dict allKeys];
  49. for (int i = 0; i < keys.count; i++)
  50. {
  51. NSString * string;
  52. if (i == 0)
  53. {
  54. string = [NSString stringWithFormat:@"%@=%@", keys[i],dict[keys[i]]];
  55. }else{
  56. //拼接时加&
  57. string = [NSString stringWithFormat:@"&%@=%@", keys[i],dict[keys[i]]];
  58. }
  59. [Url appendString:string];
  60. }
  61. return Url;
  62. }
  63. /**
  64. * 接收到服务器回应的时回调
  65. */
  66. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
  67. {
  68. if (!self.resultData)
  69. {
  70. self.resultData = [[NSMutableData alloc]init];
  71. }else
  72. {
  73. [self.resultData setLength:0];
  74. }
  75. }
  76. /**
  77. * 接收到服务器传输数据的时候调用,此方法根据数据大小执行若干次
  78. */
  79. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  80. {
  81. [self.resultData appendData:data];
  82. }
  83. /**
  84. * 数据传完之后调用此方法
  85. */
  86. - (void)connectionDidFinishLoading:(NSURLConnection *)connection
  87. {
  88. if (self.finishBlock) {
  89. self.finishBlock(self.resultData);
  90. }
  91. }
  92. /**
  93. * 网络请求过程中,出现任何错误(断网,连接超时等)会进入此方法
  94. */
  95. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
  96. {
  97. NSLog(@"network error : %@", [error localizedDescription]);
  98. if (self.failedBlock) {
  99. self.failedBlock([error localizedDescription]);
  100. }
  101. }
  102. // 将字符串,转化成数组或字典 objectFromJSONStringWithParseOptions
  103. - (id)dictionaryOrArrayWithJSONSString:(NSString *)jsonString
  104. {
  105. if (jsonString == nil)
  106. {
  107. return nil;
  108. }
  109. NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  110. return [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:nil];
  111. }
  112. #pragma mark - MD5加密
  113. +(NSString *) md5: (NSString *) inPutText
  114. {
  115. const char *cStr = [inPutText UTF8String];
  116. unsigned char result[CC_MD5_DIGEST_LENGTH];
  117. CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
  118. return [[NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  119. result[0], result[1], result[2], result[3],
  120. result[4], result[5], result[6], result[7],
  121. result[8], result[9], result[10], result[11],
  122. result[12], result[13], result[14], result[15]
  123. ] lowercaseString];
  124. }
  125. @end