ios - NSMutableURLRequest 转换为 ASIFormDataRequest

标签 ios asihttprequest nsmutableurlrequest asiformdatarequest

我已经写了fellowing代码:

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);

很好用,但是现在我想用asihttprequest框架,那么上面的代码怎么改,我写了代码,但是不能得到正确的结果,只是得到服务器的错误信息,请问这是怎么回事问题?

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);

提前致谢。

更新:

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);

我用上面的代码,也得不到同样的结果,error不为nil。

最佳答案

您的 ASIHTTP 代码与 NSURLConnection 代码做的事情不同。 ASIFormDataRequest 将自动:

这通常正是您想要的,但是如果您使用 NSURLConnection 代码获得正确的行为而使用 ASIHTTP 获得不正确的行为,那么您需要更改为 custom ASIHTTP POST 并使用 ASIHTTPRequest,而不是 ASIHTTPFormDataRequest,然后手动设置 Conten-输入回 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]];

这样做,并使用 Wireshark 准确检查发送到服务器的内容,我可以看到发送的 POST 数据仍然不完全相同(左边是 ASIHTTP,右边是 NSURLConnection):

post diff

但内容类型、长度和实际数据是相同的。 此时,我希望您的服务器返回相同的结果。 如果仍然不匹配,您可以编辑 ASIhTTP 请求参数以匹配。

关于ios - NSMutableURLRequest 转换为 ASIFormDataRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10468116/

相关文章:

ios - Imageview ios 中的 mask 和反向 mask

ios - 如何将额外参数传递给自定义 segue?

ios - iOS6 中的 ASIHTTPRequest

ios - NSMutableURLRequest 和 "request body stream exhausted"错误

ios - NSMutableUrlRequest 没有为 HTTP 请求发送正确的参数

iphone - UIPickerview-从顶部滚动

ios - NSDateFormatter dateFromString 返回错误月份的日期

iphone - ASIHTTPRequest,请求发送了两次

ios - 如何通过正确的请求删除关联的 UIProgressview?

ios - 在 iOS 上使用带有 NSMutableURLRequest 的接受 header