ASIHTTP帖子的工作方式与NSMutableURLRequest不同

时间:2012-07-06 14:00:13

标签: ios post asihttprequest nsmutableurlrequest

我曾经有过发布的方法:

-(void) execMethod:(NSString*)methodName andParams:(NSArray*)parameters withID:(NSString*)identificator {

//RPC
NSMutableDictionary* reqDict = [NSMutableDictionary dictionary];
[reqDict setObject:methodName forKey:@"method"];
[reqDict setObject:parameters forKey:@"params"];
[reqDict setObject:identificator forKey:@"id"];

//RPC JSON
NSString* reqString = [NSString stringWithString:[reqDict JSONRepresentation]];
NSData* requestData = [NSData dataWithBytes:[reqString UTF8String] length:[reqString length]];

//Request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];


//prepare http body
[request setHTTPMethod: @"POST"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody: requestData];


if (urlConnection != nil) {
    [urlConnection release];
    urlConnection = nil;
}

urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
[request release];

    }

现在我想使用ASIHttp并编写了该函数:

-(void)startRequest:(NSString*)methodName andParams:(NSArray*)parameters withID:(NSString*)identificator
   {

    NSMutableDictionary* reqDict = [NSMutableDictionary dictionary];
[reqDict setObject:methodName forKey:@"method"];
[reqDict setObject:parameters forKey:@"params"];
[reqDict setObject:identificator forKey:@"id"];

//RPC JSON
NSString* reqString = [NSString stringWithString:[reqDict JSONRepresentation]];

//Request
    NSData* requestData = [NSData dataWithBytes:[reqString UTF8String] length:[reqString length]];



asiRequest=[ASIFormDataRequest requestWithURL:url];

[asiRequest addRequestHeader:@"Accept" value:@"application/json"];
[asiRequest addRequestHeader:@"Content-Length" value:[NSString stringWithFormat:@"%d", [requestData length]]];
[asiRequest addRequestHeader:@"Content-Type" value:@"application/x-www-form-urlencoded"];

[asiRequest appendPostData:requestData];
[asiRequest setDelegate:self];

[asiRequest startAsynchronous];

}

但它的行为并不相同。它应该给我一个json。第一个正常工作但第二个没有。我的错在哪里?

由于

2 个答案:

答案 0 :(得分:0)

从您的代码示例中,您似乎提交了“application / json”类型的内容,但您将Content-Type设置为“application / x-www-form-urlencoded”。

ASIHTTP对“application / x-www-form-urlencoded”进行了特殊处理。我认为你可能遇到了这种特殊处理的问题。


查看ASI's website

之后

我发现了以下内容:

  

PUT请求和自定义POST

     

如果您想通过PUT发送数据,或者想要通过POST发送数据但更喜欢自己创建POST主体,请使用appendPostData:或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"];

因此,使用ASIHTTPRequest代替ASIFormDataRequest,您将避免特殊处理。因此,您应该能够执行以下操作(注意:不需要设置内容长度):

asiRequest = [ASIHTTPRequest requestWithURL:url];

[asiRequest addRequestHeader:@"Accept" value:@"application/json"];
[asiRequest addRequestHeader:@"Content-Type" value:@"application/x-www-form-urlencoded"];

[asiRequest setPostBody:requestData];
[asiRequest setDelegate:self];

[asiRequest startAsynchronous];

答案 1 :(得分:0)

尝试

ASIFormDataRequest

方法

setPostValue::

这是一个示例代码段

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 
NSMutableDictionary *data = [NSMutableDictionary dictionaryWithObjectsAndKeys:self.txtUsername.text, @"name", self.txtEmail.text, @"email", self.txtPassword.text, @"pwd", nil]; 
[request addRequestHeader:@"Accept" value:@"application/json"]; 
[request addRequestHeader:@"Content-Type" value:@"application/x-www-form-urlencoded"]; 
[request setRequestMethod:@"POST"]; 
for (NSString *key in data) { 
    [request setPostValue:data[key] forKey:key]; 
} 

[request setDelegate:self]; 
[request startAsynchronous];