/ 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/
Expand |
Embed | Plain Text
############################################# # URLParser.h ############################################# // @author Dimitris (09/02/2010) // @author BCmoney (05/01/2009) #import <Foundation/Foundation.h> NSArray *variables; } @end ############################################# # URLParser.m ############################################# // @author Dimitris (09/02/2010) // @author BCmoney (05/01/2009) #import "URLParser.h" @implementation URLParser @synthesize variables; self = [super init]; if (self != nil) { NSString *tempString; [scanner scanUpToString:@"?" intoString:nil]; //ignore the beginning of the string and skip to the vars while ([scanner scanUpToString:@"&" intoString:&tempString]) { [vars addObject:[tempString copy]]; } self.variables = vars; [vars release]; } return self; } if ([var length] > [varName length]+1 && [[var substringWithRange:NSMakeRange(0, [varName length]+1)] isEqualToString:[varName stringByAppendingString:@"="]]) { return varValue; } } return nil; } - (void) dealloc{ self.variables = nil; [super dealloc]; } @end ############################################# # Proxy.h ############################################# // @author BCmoney (05/01/2009) #import <Foundation/Foundation.h> NSArray *params; } @end ############################################# # Proxy.m ############################################# // @author BCmoney (05/01/2009) @implementation Proxy //parse URL URLParser *parser = [[[URLParser alloc] initWithURLString:@"http://example.com/action.php?url=100&f=text/xml&e=utf-8"] autorelease]; NSLog(@"%@", var); //URL to make HTTP request to NSLog(@"%@", format); //Format expected of response NSLog(@"%@", encoding); //Encoding expected of response (defaults to UTF-8) //prepare request [request setHTTPMethod:@"POST"]; //set headers [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; //create the POST body (optional) [postBody appendData:[[NSString stringWithFormat:@"<message>"] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[[NSString stringWithFormat:@"<service/>"] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[[NSString stringWithFormat:@"</message>"] dataUsingEncoding:NSUTF8StringEncoding]]; //do POST [request setHTTPBody:postBody]; //get response NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; NSLog(@"Response Code: %d", [urlResponse statusCode]); if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) { NSLog(@"Response: %@", result); } [parser release]; } @end
You need to login to post a comment.
