[英]NSURLRequest - encode url for NSURLRequest POST Body (iPhone objective-C)
我正在使用NSURLRequest发送帖子。
NSURL *url = [NSURL URLWithString:someUrlString];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [parameterString length]];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [parameterString dataUsingEncoding:NSUTF8StringEncoding]];
在请求正文中,我正在编码以下NVP:
returnURL = http://someSite.com
帖子成功通过,但我收到了错误。 该错误表示无效的网址。
所以,我把它改为:
RETURNURL = HTTP:%2F%2FsomeSite.com
有同样的错误。
我还与Todd Ditchendorf的HTTP客户端进行了双重检查,结果相同。
我知道我必须编码这个错误可以有人告诉我我做错了什么吗?
谢谢科里
**更新:感谢那些回答的人。 我的NVP值中错过了&符号。 当然,一旦我修好了,一切都很好。 我怀疑的是,帖子正文中的网址编码是不正确的。 我没有使用ASIHTTPRequest框架,但它确实帮助我解决了问题(对于我需要的东西而言是过度杀伤)。 **
考虑使用Ben Copsey的ASIHTTPRequest
框架来生成和发送同步和异步HTTTP请求(POST和GET):
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:@"http://someSite.com"] autorelease];
[request setPostValue:@"myValue1" forKey:@"myFormField1"];
[request setPostValue:@"myValue2" forKey:@"myFormField2"];
// etc.
[request start];
NSError *error = [request error];
if (!error)
NSString *response = [request responseString];
他的框架节省了大量时间。
我不知道这是否能解决你的问题,但是
[parameterString length]
通常不等于长度
[parameterString dataUsingEncoding:NSUTF8StringEncoding]
因为UTF8编码具有不同字节数的字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.