[SOLVED] HTTP POST
[SOLVED] HTTP POST
- Subject: [SOLVED] HTTP POST
- From: "M. Uli Kusterer" <email@hidden>
- Date: Tue, 22 Mar 2005 00:35:38 +0100
Hi folks,
I just wanted to have an app do a HTTP POST to a PHP script. Google
got lots of results at CocoaBuilder, but none seemed to have a
working solution. I finally figured out how it works, and so I
thought I'd post the code for that here for posterity.
Most of this is the same as
<http://lists.apple.com/archives/webkitsdk-dev/2004/Nov/msg00012.html>
The difference being that that code doesn't work because it considers
the dashes as part of the boundary, while the dashes have to be
prefixed to the boundary everywhere *except* in the Content-Type
header.
-(void) applicationDidFinishLaunching: (NSNotification*)notif
{
NSURL *cgiUrl = [NSURL
URLWithString:@"http://server.local/cgitester.php"];
NSMutableURLRequest *postRequest = [NSMutableURLRequest
requestWithURL: cgiUrl];
NSString *boundary = @"0xKhTmLbOuNdArY";
NSString *contentType = [NSString
stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
// setting the headers:
[postRequest setHTTPMethod:@"POST"];
[postRequest setValue: contentType forHTTPHeaderField: @"Content-Type"];
// adding the body:
NSMutableData *postBody = [NSMutableData data];
[postBody appendData: [[NSString
stringWithFormat:@"--%@\r\n",boundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData: [@"Content-Disposition: form-data;
name=\"realname\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData: [@"Joe Doe" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData: [[NSString
stringWithFormat:@"\r\n--%@\r\n",boundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData: [@"Content-Disposition: form-data;
name=\"email\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData: [@"email@hidden"
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData: [@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postRequest setHTTPBody:postBody];
[NSURLConnection connectionWithRequest: postRequest delegate: self];
}
-(void) connection: (NSURLConnection*)connection didReceiveData: (NSData*)data
{
NSString* str = [[[NSString alloc] initWithData: data encoding:
NSUTF8StringEncoding] autorelease];
// Do a [[[field textStorage] mutableString] appendString: str];
here or whatever...
}
Ain't the web great? :-)
--
Cheers,
M. Uli Kusterer
------------------------------------------------------------
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de
_______________________________________________
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