Posted By

bcmoney on 12/10/10


Tagged

http Objective-c iphone proxy


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

terababy
Tintenklecks
cnathan


proxy.m


 / Published in: Objective C
 

Lovingly reposted sections of code from "iphone HTTP request": http://snipplr.com/view/22599/iphone-http-request/ -amd- http://iphone.demay-fr.net/2010/04/parsing-url-parameters-in-a-nsstring/

  1. #############################################
  2. # URLParser.h
  3. #############################################
  4. // @author Dimitris (09/02/2010)
  5. // @author BCmoney (05/01/2009)
  6. #import <Foundation/Foundation.h>
  7.  
  8. @interface URLParser : NSObject {
  9. NSArray *variables;
  10. }
  11.  
  12. @property (nonatomic, retain) NSArray *variables;
  13.  
  14. - (id)initWithURLString:(NSString *)url;
  15. - (NSString *)valueForVariable:(NSString *)varName;
  16.  
  17. @end
  18.  
  19.  
  20.  
  21. #############################################
  22. # URLParser.m
  23. #############################################
  24. // @author Dimitris (09/02/2010)
  25. // @author BCmoney (05/01/2009)
  26. #import "URLParser.h"
  27.  
  28. @implementation URLParser
  29. @synthesize variables;
  30.  
  31. - (id) initWithURLString:(NSString *)url{
  32. self = [super init];
  33. if (self != nil) {
  34. NSString *string = url;
  35. NSScanner *scanner = [NSScanner scannerWithString:string];
  36. [scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"&?"]];
  37. NSString *tempString;
  38. [scanner scanUpToString:@"?" intoString:nil]; //ignore the beginning of the string and skip to the vars
  39. while ([scanner scanUpToString:@"&" intoString:&tempString]) {
  40. [vars addObject:[tempString copy]];
  41. }
  42. self.variables = vars;
  43. [vars release];
  44. }
  45. return self;
  46. }
  47.  
  48. - (NSString *)valueForVariable:(NSString *)varName {
  49. for (NSString *var in self.variables) {
  50. if ([var length] > [varName length]+1 && [[var substringWithRange:NSMakeRange(0, [varName length]+1)] isEqualToString:[varName stringByAppendingString:@"="]]) {
  51. NSString *varValue = [var substringFromIndex:[varName length]+1];
  52. return varValue;
  53. }
  54. }
  55. return nil;
  56. }
  57.  
  58. - (void) dealloc{
  59. self.variables = nil;
  60. [super dealloc];
  61. }
  62.  
  63. @end
  64.  
  65.  
  66.  
  67.  
  68. #############################################
  69. # Proxy.h
  70. #############################################
  71. // @author BCmoney (05/01/2009)
  72. #import <Foundation/Foundation.h>
  73.  
  74. @interface Proxy : NSObject {
  75. NSArray *params;
  76. }
  77.  
  78. @property (nonatomic, retain) NSArray *variables;
  79.  
  80. - (id)makeRequest:(NSString *)url;
  81. - (NSString *)valueForVariable:(NSString *)url;
  82. - (NSString *)valueForVariable:(NSString *)f;
  83. - (NSString *)valueForVariable:(NSString *)e;
  84.  
  85. @end
  86.  
  87.  
  88. #############################################
  89. # Proxy.m
  90. #############################################
  91. // @author BCmoney (05/01/2009)
  92.  
  93. @implementation Proxy
  94.  
  95. //parse URL
  96. URLParser *parser = [[[URLParser alloc] initWithURLString:@"http://example.com/action.php?url=100&f=text/xml&e=utf-8"] autorelease];
  97. NSString *url = [parser valueForVariable:@"url"];
  98. NSLog(@"%@", var); //URL to make HTTP request to
  99. NSString *format = [parser valueForVariable:@"f"];
  100. NSLog(@"%@", format); //Format expected of response
  101. NSString *encoding = [parser valueForVariable:@"e"];
  102. NSLog(@"%@", encoding); //Encoding expected of response (defaults to UTF-8)
  103.  
  104.  
  105. - (id) makeRequest:(NSString *)url{
  106. //prepare request
  107. NSString *urlString = [NSString stringWithFormat:parser];
  108. NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
  109. [request setURL:[NSURL URLWithString:urlString]];
  110. [request setHTTPMethod:@"POST"];
  111.  
  112. //set headers
  113. NSString *contentType = [NSString stringWithFormat:format];
  114. [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
  115.  
  116. //create the POST body (optional)
  117. NSMutableData *postBody = [NSMutableData data];
  118. [postBody appendData:[[NSString stringWithFormat:@"<message>"] dataUsingEncoding:NSUTF8StringEncoding]];
  119. [postBody appendData:[[NSString stringWithFormat:@"<service/>"] dataUsingEncoding:NSUTF8StringEncoding]];
  120. [postBody appendData:[[NSString stringWithFormat:@"</message>"] dataUsingEncoding:NSUTF8StringEncoding]];
  121.  
  122. //do POST
  123. [request setHTTPBody:postBody];
  124.  
  125. //get response
  126. NSHTTPURLResponse* urlResponse = nil;
  127. NSError *error = [[NSError alloc] init];
  128. NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
  129. NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  130. NSLog(@"Response Code: %d", [urlResponse statusCode]);
  131. if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
  132. NSLog(@"Response: %@", result);
  133. printf(result); //here we output the response
  134. }
  135.  
  136. [parser release];
  137. }
  138.  
  139. @end

Report this snippet  

You need to login to post a comment.