0

我想使用 ASIHTTPRequest appendPostData 方法将字符串数据发送到 php 服务器,但它不起作用。

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:[@"123456" dataUsingEncoding:NSUTF8StringEncoding]];
[request startAsynchronous];

我尝试对请求本身进行一些修改,例如:

[request setRequestMethod:@"POST"];
[request buildPostBody];

但这也行不通。

当我使用

[request addPostValue:@"Ben" forKey:@"names"];

语法它确实有效。

有人知道这里有什么问题吗?!

4

1 回答 1

1

我通常使用这个:

- (void)performPostRequestWithString:(NSString *)string stringDictionary:(NSDictionary *)stringDictionary dataDictionary:(NSDictionary *)dataDictionary delegate:(id)requestDelegate requestSelector:(SEL)requestSelector errorSelector:(SEL)errorSelector {

    //localCopy = self;

    self.delegate = requestDelegate;
    self.callback = requestSelector;
    self.errorCallback = errorSelector;

    NSURL *url = [NSURL URLWithString:string];

    postRequest = [[ASIFormDataRequest alloc] initWithURL:url];
    [postRequest setDelegate:self];
    [postRequest setRequestMethod:@"POST"];

    if (stringDictionary)
        for (NSString *key in [stringDictionary allKeys])
            [postRequest setPostValue:[stringDictionary objectForKey:key] forKey:key];

    if (dataDictionary)
        for (NSString *key in [dataDictionary allKeys])
            [postRequest setData:[dataDictionary objectForKey:key] forKey:key];

    //NSLog(@"request url = %@", [postRequest.url absoluteString]);
    [postRequest startAsynchronous];
}

参数:

  • (NSString *)string- url 字符串,在哪里发布您的请求;
  • (NSDictionary *)stringDictionary- 字典,包含所有文本信息(如姓名、ID 等);
  • (NSDictionary *)dataDictionary- 字典,包含所有数据信息(如照片、文件等);
  • (id)requestDelegate- 委托执行以下选择器;
  • (SEL)requestSelector- 选择器,将在成功请求时执行;
  • (SEL)errorSelector- 选择器,将在发生错误时执行。

PS 选择器将在ASIHTTPRequest Delegate实现中使用

于 2012-04-12T07:51:58.880 回答