开发者

How to create Http Post data to the web server?

How to create Http Post data to the web server ?

Do you hav开发者_StackOverflowe an example related this issue ?

Thanks in advance.


It depends on what you want to send to the server. for instance: Image file or just Text(like that from a http form).

For an image file that is in the format UIIMage:

NSData *imageData = [NSData dataWithContentsOfFile:ImagePath];

or

NSData *imageData = UIImageJPEGRepresentation(myImage.image, 1.0);

Set your remote url(for instance if you are using php)

NSString *urlString = "http://yourdomain.com/yourphpfile.php" 

The rest of the code:

NSMutableURLRequest *request2 = [[[NSMutableURLRequest alloc] init] autorelease];
            [request2 setURL:[NSURL URLWithString:urlString]];
            [request2 setHTTPMethod:@"POST"];

            NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
            NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
            [request2 addValue:contentType forHTTPHeaderField: @"Content-Type"];


NSMutableData *body = [NSMutableData data];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    

[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name='userfile'; filename='"] dataUsingEncoding:NSUTF8StringEncoding]];
//give any name for your file(this case image file example)         
[body appendData:[[NSString stringWithString:pic_filename.jpg] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithString:@"'\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];


//add the file data to the http body            
    [body appendData:[NSData dataWithData:imageData]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// setting the body of the post to the reqeust
[request2 setHTTPBody:body];

The case of Just Text(Similar to the $_GET in php)

Prepare the data you want to send. For the first name from a form for instance

//You have your first name stored in the_first_name_to_send variable

NSString *f_name = nil;
f_name = [[NSString alloc] initWithFormat:@"%@", the_first_name_to_send];

//Encoding conversion
//This is optional but it ensures that every character encoding type is sent without issues
NSString* escapedUrlStringFirstName =[f_name stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

NSString *string1 = @"firstName=";

NSString *myRequestString;
        myRequestString = [string1 stringByAppendingString:escapedUrlStringFirstName];
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length ] ];

NSMutableURLRequest *request2 = [[[NSMutableURLRequest alloc] init] autorelease];

[request2 setHTTPBody: myRequestData ];

[request2 setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];

NSData *returnData2 = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];

returnData2 = nil;

Uploading image file (the server side yourphpfile.php)

$file_tmp = $_FILES['userfile']['tmp_name'];
$filename = $_FILES['userfile']['name'];

Sending text using http post (the server side yourphpfile.php)

$fname = $_POST["firstName"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜