iPhone: Testing Browser (HTTP Headers)

I am trying to write a short application that handles logging into a 3rd-party website automatically and then open's Safari. I mimic a submission of the form on the login page and send an NSURLRequest with the POST method and all the variables url-encoded in the body. This works on one website. Now, I want to do the same for a different website. So I plugged in a different URL and the different POST variables. It doesnt seem to work on this site. I even modified the code to capture any cookies that were being returned in the NSHTTPURLResponse and to add them to the NSHTTPCookieStorage shared by all applications before I launch Safari. It would be useful to see exactly the responses from the server in order to figure out what is going on, but I want Safari to handle the responses. Any way someone can think of accomplishing this?

Here is the code i am currently using:

@implementation SubmitAndLaunch

@synthesize postVariables;
@synthesize postDestination;
@synthesize redirectedURL;

- (void)connectAndSubmit {
if (postVariables == nil | postDestination == nil) {
// Die
[NSException raise:@"InvalidVarsDest" format:@"The Variables or Destination were not defined"];
}
NSData *postData = [postVariables dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:postDestination]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPShouldHandleCookies:YES];
[request setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9, /;q=0.8" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:postData];
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
[conn start];
}

- (id)initWithVariables:(NSString *)vars Destination:(NSString *)dest {
self = [super init];
if (self != nil) {
postVariables = vars;
postDestination = dest;
[self connectAndSubmit];
}
return self;
}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
redirectedURL = [response URL];
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSDictionary *header = [httpResponse allHeaderFields];
NSMutableArray *ourCookieArray = [NSHTTPCookie cookiesWithResponseHeaderFields:header forURL:redirectedURL];
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
[storage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
for (int i=0; i< [ourCookieArray count]; i++) {
[storage setCookie:[ourCookieArray objectAtIndex:i]];
}
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[redirectedURL absoluteString]]];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
}

- (void)dealloc{
[redirectedURL dealloc];
[postVariables dealloc];
[postDestination dealloc];
[super dealloc];
}

@end

15in MBP C2D, Mac OS X (10.4.9)

Posted on Jun 24, 2008 3:23 PM

Reply
3 replies

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

iPhone: Testing Browser (HTTP Headers)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.