如何使用请求向php脚本发布请求并在目标C中发布变量

时间:2012-05-22 20:06:17

标签: php ios

我想将一个来自Objective-C代码的帖子请求发送到一个带有URL

的php脚本

http://ggg.abc.com/example.php?MO&email=test@gmail.com&message=TESTMSG&recipient=11111

我尝试使用ASIFormDataRequest,如下所示,但如何在请求中指定请求变量MO?

NSURL* url = [NSURL URLWithString:ServerApiURL];
ASIFormDataRequest* request = [ASIFormDataRequest requestWithURL:url];
[request setDelegate:self];

// Add the POST fields
//[request setPostValue:@"" forKey:@"MO"];
//[request setRequestMethod:@"MO"];

[request setPostValue:@"test@gmail.com" forKey:@"email"]; 
[request setPostValue:@"TESTMSG" forKey:@"message"];
[request setPostValue:@"11111" forKey:@"recipient"];

或者其他任何方式吗?

1 个答案:

答案 0 :(得分:0)

作为最后的手段,-appendPostData:应该可以工作。

PUT requests and custom POSTs


如果您想通过PUT发送数据,或者想要通过POST发送数据但更喜欢自己创建POST主体,请使用appendPostData:或appendPostDataFromFile:。

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:[@"This is my data" dataUsingEncoding:NSUTF8StringEncoding]];
// Default becomes POST when you use appendPostData: / appendPostDataFromFile: / setPostBody:
[request setRequestMethod:@"PUT"];

如果您想发送大量数据但未使用ASIFormDataRequest,请参阅下面有关磁盘邮件正文的信息。


对你而言,这将是:

NSURL* url = [NSURL URLWithString:ServerApiURL];
ASIFormDataRequest* request = [ASIFormDataRequest requestWithURL:url];
[request setDelegate:self];
[request appendPostData:[@"MO&email=test@gmail.com&message=TESTMSG&recipient=11111" dataUsingEncoding:NSUTF8StringEncoding]];

祝你好运


更新最新评论的答案

NSURL* url = [NSURL URLWithString:ServerApiURL];
NSURL* urlWithParams = [NSURL URLWithString:@"?MO&email=test@gmail.com&message=TESTMSG&recipient=11111" relativeToURL:url];
ASIFormDataRequest* request = [ASIFormDataRequest requestWithURL:urlWithParams];
[request setDelegate:self];
// Does this request need to be a post? If so, then call -setRequestMethod:.
// NOTE: I don't know if zero length posts work.
[request setRequestMethod:@"POST"];