如何创建到Web服务器的Http Post数据? [英] How to create Http Post data to the web server?

查看:99
本文介绍了如何创建到Web服务器的Http Post数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建到Web服务器的Http Post数据?

How to create Http Post data to the web server ?

您是否有与此问题相关的示例?

Do you have an example related this issue ?

预先感谢。

推荐答案

这取决于您要发送到服务器的内容。例如:图像文件或仅是文本(例如来自http表单的文本)。

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

对于格式为UIIMage的图像文件:

For an image file that is in the format UIIMage:

NSData *imageData = [NSData dataWithContentsOfFile:ImagePath];

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

设置您的远程URL(例如,如果您使用php)

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

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

其余代码:

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];

Just Text(类似于php中的$ _GET)

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;

上传图像文件(服务器端yourphpfile.php)

Uploading image file (the server side yourphpfile.php)

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

使用http post发送文本(服务器端yourphpfile.php)

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

$fname = $_POST["firstName"];

这篇关于如何创建到Web服务器的Http Post数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆