通过HTTP Post发送表单数据[Objective C] [英] Sending form data via HTTP Post [Objective C]

查看:143
本文介绍了通过HTTP Post发送表单数据[Objective C]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自iphone应用程序的表单数据需要将HTTP Post变量发送到URL。

I have a form data from an iphone app that needs to send HTTP Post variables to a URL.

事情是,我被告知不要使用setHTTPBody,但我不确定如何做到这一点,因为我看到的每个例子都使用setHTTPBody;甚至那些使用POST方法的人。

The thing is, I've been told not to use setHTTPBody, but am unsure of how else to do it as every example I see uses setHTTPBody; even those that use POST methods.

为了使事情变得更复杂,URL /网页需要 submit = true action = inquiry

To make things matters a little more complicated, the URL/webpage requires a submit=true and action=enquiry in the post variables.

这是我到目前为止所拥有的。

Here's what I have so far.

NSString *formContactName   = [self contactName];
NSString *formEmail     = [self email];
NSString *formPhone     = [self phone];
NSString *formComments      = [self comments];

// The above data is fine, as explored here.
NSLog(@"formContactName = %@", formContactName);
NSLog(@"formEmail = %@", formEmail);
NSLog(@"formPhone = %@", formPhone);
NSLog(@"formComments = %@", formComments);

// Setup request
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:advertURL]];
[request setHTTPMethod:@"POST"];

// set headers
NSString *contentType = [NSString stringWithFormat:@"text/plain"];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];

// setup post string
NSMutableString *postString = [[NSMutableString alloc] init];   
[postString appendFormat:@"contactname=%@", formContactName];
[postString appendFormat:@"&email=%@", formEmail];
[postString appendFormat:@"&phone=%@", formPhone];
[postString appendFormat:@"&comments=%@", formComments];
[postString appendFormat:@"&submit=yes"];
[postString appendFormat:@"&action=enquiry"];

// I've been told not to use setHTTPBody for post variables, but how else do you do it?
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

// get response
NSHTTPURLResponse *urlResponse = nil;
NSError *error = [[NSError alloc] init];

NSData *responseData = [NSURLConnection sendSynchronousRequest:request 
                         returningResponse:&urlResponse 
                                     error:&error];

NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];


NSLog(@"Response code: %d", [urlResponse statusCode]);
if ([urlResponse statusCode] >=200 && [urlResponse statusCode] <300)
{
NSLog(@"Response ==> %@", result);
}

[result release];
[error release];

我总是得到200的结果,这很好。但回复表明Post变量没有通过。

I always get a 200 result, which is good. But the response back indicates that the Post variables are not getting through.

回复中有一个名为enquirysent的字段,它总是空白。如果Post变量成功,则应返回1或真实语句。

The response back has a field in it called "enquirysent" and its always blank. If the Post variables are successful, it should return a "1" or true statement.

"enquirysent": ""

因此,我的问题是:如何在上述请求中强制使用HTTP-POST变量?

Therefore, my question is: How do I force the HTTP-POST variables in the above request?

谢谢。

推荐答案

你可以使用 ASIHTTPRequest 简化:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"Ben" forKey:@"first_name"];
[request setPostValue:@"Copsey" forKey:@"last_name"];
[request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"];

这篇关于通过HTTP Post发送表单数据[Objective C]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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