0

我想将来自 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"];

或任何其他方式来做到这一点?

4

1 回答 1

0

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

PUT 请求和自定义 POST


如果您想通过 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"];
于 2012-05-22T23:04:15.407 回答