duanfu3884 2012-05-22 20:06
浏览 22
已采纳

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

I want to send a post request from objective-C code to a php script with URL

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?

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?

  • 写回答

1条回答 默认 最新

  • douhei8633 2012-05-22 23:04
    关注

    As a last resort, -appendPostData: should work.

    PUT requests and custom POSTs


    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:.

    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.


    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"];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持