为什么我用TTURLRequest,post数据在服务器上解析不了啊


我如下代码发送请求,为什么在服务其上。用request.getParamter("name")
那不到值呢?

TTURLRequest *request = [self request:url Delegate:delegate];
[request setHttpMethod:@"POST"];
[request.parameters setObject:@"agassi_yzh" forKey:@"name"];
[request send];

three20 ios

mykey 10 years, 8 months ago

自己把问题解决了:

TTURLRequest的parameters中的数据设置的格式是发送文件的报文形式。

如果要用request.getParamter来解析。需要自己构造post的httpbody

这么写:

TTURLRequest *request = [self request:url Delegate:delegate];
    [request setHttpMethod:@"POST"];
    request.contentType=@"application/x-www-form-urlencoded";

    NSMutableString* postBodyString = [NSMutableString stringWithString:@""];

    if (nil != info) {
        [request setUserInfo:[NSDictionary dictionaryWithDictionary:info]];
    }

    if (nil != params) {
        for (NSString *key in [params allKeys]) {
            NSString* p = [NSString stringWithFormat:@"%@=%@",key,[[params valueForKey:key] URLEncodedString]];
            postBodyString = [NSString stringWithFormat:@"%@&%@",postBodyString,p];
        }
    }

    NSData* postData = [NSData dataWithBytes:[postBodyString UTF8String] length:[postBodyString length]];

    [request setHttpBody:postData];

    [request send];
無良消毒液 answered 10 years, 8 months ago

Your Answer