Search code examples
iosasihttprequestnsmutableurlrequestasiformdatarequest

NSMutableURLRequest transform to ASIFormDataRequest


I have writen the fellowing code:

NSString *urlString = [NSString stringWithFormat:ADDRESS,action];
postStr = @"user_name=Thomas Tan&phone=01234567891&password=123456";
NSData *myRequestData = [NSData dataWithBytes:[postStr UTF8String] length:[postStr length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody: myRequestData];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *responseString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; 
NSLog(@"%@",responseString);

it works well,but now I want to use asihttprequest framework,so how to change the above code,I have writen the code,but it can't get the correct result and just get the server error infomation.so what's the problem?

NSString *urlString = [NSString stringWithFormat:ADDRESS,action];

NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *requeset = [ASIFormDataRequest requestWithURL:url];
[requeset setRequestMethod:@"POST"];
[requeset setPostValue:@"Thomas Tan" forKey:@"user_name"];
[requeset setPostValue:@"01234567891" forKey:@"phone"];
[requeset setPostValue:@"123456" forKey:@"password"];
[requeset startSynchronous];
NSError *error = [requeset error];
if (!error) {
    NSString *re = [requeset responseString];
    NSLog(@"%@",re);
}
NSLog(@"%@",error);

thank you in advance.

UPDATE:

NSString *urlString = [NSString stringWithFormat:ADDRESS,action];
NSURL *url = [NSURL URLWithString:urlString];

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:@"POST"]; 
[request appendPostData:[@"user_name=Thomas Tan&phone=01234567891&password=123456" dataUsingEncoding:NSUTF8StringEncoding]];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
    NSString *re = [request responseString];
    NSLog(@"%@",re);
}
NSLog(@"%@",error);

I use the above code ,It also can't get the same result,and error is not nil.


Solution

  • Your ASIHTTP code is not doing the same thing as your NSURLConnection code. ASIFormDataRequest will automatically:

    That's usually exactly what you want, but if you're getting the correct behavior with your NSURLConnection code and incorrect with ASIHTTP, then you need to change to a custom ASIHTTP POST and use ASIHTTPRequest, not ASIHTTPFormDataRequest, and then manually set the Conten-type back to application/x-www-form-urlencoded:

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setRequestMethod:@"POST"]; 
    [request addRequestHeader:@"Content-Type" value:@"application/x-www-form-urlencoded"];
    [request appendPostData:[@"user_name=Thomas Tan&phone=01234567891&password=123456" dataUsingEncoding:NSUTF8StringEncoding]];
    

    Doing this, and inspecting exactly what was sent to the server using Wireshark, I can see that the POST data sent is still not quite identical (ASIHTTP on the left, NSURLConnection on the right):

    post diff

    But the content type, length, and actual data is identical. At this point, I'd expect your server to return the same result. If it still doesn't, you can edit the ASIhTTP request parameters to match.