• 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 12:51:27 -0400

For starters, Apple isn't set up for arbitrary requests from Cocoa code so @"testing 1, 2, 3" probably isn't something they're going to handle : )

For KHTTPSocket to work, it needs something on the other end to talk to. I don't know Java so can't help you there. You'll have to write something that can get data out of a POST, and "echo" (PHP term) it back.

Although not Cocoa/Java, I have a Javascript/PHP example at "javascript/codozoa.com/SocketTest.zip" (.zip archive) that shows the basic mechanics of what needs to be done. It might help you figure out what you need to do in your Java on the server end.

HTH
Ken


On Sep 14, 2006, at 6:17 AM, deepak gopal wrote:

Hi Ken ,

I tried modifying your code as

NSURL *url = [NSURL URLWithString:@"http://www.apple.com";];

NSData *data; // data from the website
KHTTPSocket     *socket        = [KHTTPSocket socketWithURL: url];

id                result        = [socket send: @"testing 1, 2, 3"];

But when I run the code, I get the following error:

NSURLErrorDomain
Error Code : 1012
User info : 2 key/value paris

Please help me out.

THANKS,
DEEP


NKen Tozier <email@hidden> wrote: 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


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


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: This email sent to email@hidden
  • Follow-Ups:
    • Re: POSTing into a server from COCOA
      • From: Ken Tozier <email@hidden>
References: 
 >Re: POSTing into a server from COCOA (From: deepak gopal <email@hidden>)

  • Prev by Date: move application to secondary screen
  • Next by Date: Re: NSTextField not updating NSUserDefaults using bindings
  • Previous by thread: Re: POSTing into a server from COCOA
  • Next by thread: Re: POSTing into a server from COCOA
  • Index(es):
    • Date
    • Thread