[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details

How to POST and PARSE HTTP Data in Objective C

Asked by CapitalQ in Objective-C Programming Language, iPhone, Cocoa Programming Language

Tags: iphone, obj c, obkective c, cocoa touch, cocoa

I have written Windows/PocketPC based applications to communicate with various payment gateways.  And am now looking to provide same for the iPhone.

The only trouble I am having is with the POST to the gateway.  In .Net we use something like this:
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(post_url);
            objRequest.Method = "POST";
            objRequest.ContentLength = post_string.Length;
            objRequest.ContentType = "application/x-www-form-urlencoded";

            // post data is sent as a stream
            StreamWriter myWriter  = null;
            myWriter = new StreamWriter(objRequest.GetRequestStream());
            myWriter.Write(post_string);
            myWriter.Close();

            // returned values are returned as a stream, then read into a string
            String post_response;
            HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
            using (StreamReader responseStream = new StreamReader(objResponse.GetResponseStream()) )
            {
                post_response = responseStream.ReadToEnd();
                responseStream.Close();
            }

I am using the code below and the returned data is HTML not the data expected.  In .Net you form the request and then create a StreamReader to grab the data.  Am I missing a property from the NSHTTPURLResponse??  Or am I going about the wrong way??
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
-(void) createRequest 
{
	NSLog(@"Made it to createRequest");
	NSData *postData= [myString dataUsingEncoding: NSASCIIStringEncoding];
	NSString *postLength=[NSString stringWithFormat: @"%d",[postData length]];
	NSError *error;
	NSHTTPURLResponse *response;
	
	vmURL= [NSURL URLWithString:@"https://www.gateway.com/ProcessCard.do" ];
	vmRequest=[NSMutableURLRequest requestWithURL:vmURL];
	[vmRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
	[vmRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
	
	[vmRequest setHTTPMethod:@"POST"];
	NSLog(@"create   %@:%@",[myString class],[myString dataUsingEncoding: NSASCIIStringEncoding]);
	[vmRequest setHTTPBody: postData];
	
	NSData * data = [NSURLConnection sendSynchronousRequest:vmRequest returningResponse: &response error: &error];  
	NSLog(@"\n \n%@",data);
	NSMutableString *str;
	str=[[NSString alloc] initWithData: data encoding: NSASCIIStringEncoding];
	NSLog(@"\n \n \n %@",str);
}
[+][-]07/12/09 10:03 PM, ID: 24837252Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/22/09 10:11 AM, ID: 24917313Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/22/09 10:18 PM, ID: 24922010Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92 - Hierarchy / EE_QW_3_20080625