0

我曾经有一个方法来发布:

-(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。第一个工作正常,但第二个没有。我的错在哪里?

谢谢

4

2 回答 2

0

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

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


在查看了ASI 的网站后

我发现了以下内容:

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];
于 2012-07-06T14:11:41.017 回答
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];
于 2015-11-18T06:45:26.050 回答