开发者

iPhone ASIHttpRequest - can't POST variables asynchronously

开发者 https://www.devze.com 2023-02-10 17:06 出处:网络
Greetings, I\'m trying to simply POST data to a url using ASIHttpRequest. Here is my code: __block ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url];

Greetings,

I'm trying to simply POST data to a url using ASIHttpRequest.

Here is my code:

__block ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url];
[request setPostBody:[NSMutableData dataWithData:[@"uname=Hello" dataUsingEncoding:NSUTF8StringEncoding]]];
[request setDelegate:self];
[request setCompletionBlock:^{
    NSString *response=[request responseString];

    UIAlertView *msg=[[UIAlertView alloc] initWithTitle:@"Response" message:response delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [msg show];
    [msg release];
}];
[request setFailedBlock:^{
    NSError *error =[request error];
}];
[request startAsynchronous];

Basically, my url is xxx.xxx.开发者_JAVA百科xxx.xxx/login.php, when I dump the PHP $_POST variable, I just get an empty array - i.e. no POST parameters are sent! The rest of the PHP is tested and working fine.

I've looked through the allseeing-i.com documentation and examples and can't seem to resolve this problem.

Any insight greatly appreciated.

Many thanks in advance,


I am having the exact same problem. I am using ASIHTTPRequest and trying to set my own POST data. I have tried both [request setPostBody:] and [request appendPostData:].

When I run these lines just before I start the request, I find that both the method and the data are what I expect.

NSLog(@"%@", [request requestMethod]);
NSLog(@"%@", [[[NSString alloc] initWithBytes:[[request postBody] bytes]
                                      length:[[request postBody] length]
                                    encoding:NSUTF8StringEncoding] autorelease]);

When I send it to the server, however, the request is made and logged but the POST data is empty.

I have, however, gotten my code working by switching to ASIFormDataRequest.

Reading the documentation, however, suggests to me that what you and I are doing should be working, so I suspect that is a bug in ASIHTTPRequest and I will contact the author to see if this is the case.

Update

One possibility is that the code is redirecting to another URL. In that case, the post data may be dropped. If that is the case, you can try using

[request setShouldUseRFC2616RedirectBehaviour:YES];⠀⠀⠀

which will allow the request to send the post data to the redirected URL.


I believe you need to setup the callback functions for async.

-(void)requestFinished:(ASIHTTPRequest *)request{
   NSLog([request responseString]);
}

edit: looking again, u had some completion block code there... never seen that before, but maybe that takes care of what i posted above

0

精彩评论

暂无评论...
验证码 换一张
取 消