搜尋

首頁  >  問答  >  主體

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

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

TTURLRequest *request = [self request:url Delegate:delegate];
[request setHttpMethod:@"POST"];
[request.parameters setObject:@"agassi_yzh" forKey:@"name"];
[request send];
PHP中文网PHP中文网2772 天前644

全部回覆(1)我來回復

  • 大家讲道理

    大家讲道理2017-04-17 11:00:06

    自己把问题解决了:

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

    回覆
    0
  • 取消回覆