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 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。