ios - 为什么我用TTURLRequest,post数据在服务器上解析不了啊
PHP中文网
PHP中文网 2017-04-17 10:58:06
0
1
645

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

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

认证高级PHP讲师

Antworte allen(1)
大家讲道理

自己把问题解决了:

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];
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage