简体   繁体   English

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

[英]How to post request to php script with request and post variables in objective C

I want to send a post request from objective-C code to a php script with URL 我想将目标C代码中的发布请求发送到带有URL的php脚本

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

I tried to use ASIFormDataRequest as below, but how do I specify the request variable MO in the request? 我尝试如下使用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"];

Or any other way to do this? 或任何其他方式做到这一点?

As a last resort, -appendPostData: should work. 作为最后的选择,-appendPostData:应该起作用。

PUT requests and custom POSTs PUT请求和自定义POST


If you want to send data via PUT, or want to send it with POST but prefer to create the POST body yourself, use appendPostData: or appendPostDataFromFile:. 如果要通过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"];

If you want to send large amounts of data and aren't using an ASIFormDataRequest, see the information on streaming post bodies from disk below. 如果要发送大量数据并且不使用ASIFormDataRequest,请参见下面有关从磁盘流式传输消息正文的信息。


For you this would be: 对于您来说,这将是:

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

Good Luck 祝好运


Update answer for lastest comment 更新答案以获取最新评论

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM