• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: POSTing into a server from COCOA
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: POSTing into a server from COCOA


  • Subject: Re: POSTing into a server from COCOA
  • From: Ken Tozier <email@hidden>
  • Date: Thu, 14 Sep 2006 02:06:36 -0400


On Sep 14, 2006, at 1:12 AM, deepak gopal wrote:

Hi All

I am really confused I can't find a good way to connect to the my server and send a request to it. I have been spending a lot of time on this without getting any results.

Can anyone suggest/post a sample code with the same functionalities?

Thank You
Deep

Hi Deep

I wrote this little class to encapsulate HTTPRequests using POST and it's been working nicely for several months. It doesn't deal with Java specifically, but it shouldn't require too many tweaks

@interface KHTTPSocket : NSObject
{
	NSURL		*url;
}

+ (id) socketWithURL:(NSURL *) inURL;
- (id) initSocketWithURL:(NSURL *) inURL;

- (id) send:(id) inData;

@end

@implementation KHTTPSocket

+ (id) socketWithURL:(NSURL *) inURL
{
	return [[[KHTTPSocket alloc]
				initSocketWithURL: inURL]
				autorelease];
}

- (id) initSocketWithURL:(NSURL *) inURL
{
	if (self = [super init])
	{
		url			= [inURL retain];
	}

	return self;
}

- (void) dealloc
{
	[url release];

	[super dealloc];
}

- (id) send:(NSString *) inData
{
// serialize inData into PHP form
// NOTE: "query=" prefix is what the PHP script looks for in the $_POST
// variable to see if there's anything for it to process
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url];

const char *temp = [inData UTF8String];

NSData *body = [NSData dataWithBytes: temp length: strlen(temp)],
*result;

NSString *bodyLength = [[NSNumber numberWithInt: [body length]] stringValue];

NSCharacterSet *returnChars = [NSCharacterSet characterSetWithCharactersInString: @"\r"];

NSHTTPURLResponse *response;

NSError *error;

NSLog(@"php query string = %@", phpData);

// generate the HTTP POST
[request setHTTPMethod: @"POST"];
[request setValue: @"application/x-www-form-urlencoded" forHTTPHeaderField: @"Content-type"];
[request setValue: bodyLength forHTTPHeaderField: @"Content-length"];
[request setValue: @"close" forHTTPHeaderField: @"Connection"];
[request setHTTPBody: body];


// send it away
result = [NSURLConnection sendSynchronousRequest: request
returningResponse: &response
error: &error];

NSLog(@"result = %@, error = %@", result, error);

// process result if any
if ((result != nil) && ([result length] > 0))
{
// You may need to change this part. I use it with PHP scripts that return PList formatted data
NSString *temp = [[[NSString alloc] initWithBytes: [result bytes]
length: [result length]
encoding: NSUTF8StringEncoding]
stringByTrimmingCharactersInSet: returnChars];

return [temp propertyList];
}
else if (error != NULL)
{
// something went awry log HTTP header and error
NSLog(@"response = %@", [response allHeaderFields]);
NSLog(@"error = %@", error);
}

return nil;
}



@end

And to use it

KHTTPSocket	 *socket		= [KHTTPSocket socketWithURL: inURL];

id result = [socket send: @"testing 1, 2, 3"]; // <- do something with the data in your server side java

NSLog(@"result = %@", result);


HTH

Ken




---------------------------------
Find out what India is talking about on - Yahoo! Answers India
Send FREE SMS to your friend's mobile from Yahoo! Messenger Version 8. Get it NOW
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40comcast.net


This email sent to email@hidden

_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: POSTing into a server from COCOA
      • From: deepak gopal <email@hidden>
References: 
 >POSTing into a server from COCOA (From: deepak gopal <email@hidden>)

  • Prev by Date: Re: POSTing into a server from COCOA
  • Next by Date: GCOV question
  • Previous by thread: Re: POSTing into a server from COCOA
  • Next by thread: Re: POSTing into a server from COCOA
  • Index(es):
    • Date
    • Thread