NSURLRequest - 编码NSURLRequest的URL url POST Body(iPhone objective-C) [英] 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客户端,具有相同的结果。
我知道我必须编码这个错误,有人可以告诉我我做错了什么?
感谢
Corey
**更新:
感谢那些回答。我错过了一个&符号在我的NVP值。所以当然,一旦我修复它,一切都很好。我怀疑,在身体的一个url的编码是不正确的。我不使用ASIHTTPRequest框架,但它确实帮助我解决问题(overkill我需要的)。
**
请考虑使用Ben Copsey的 ASIHTTPRequest
框架用于生成和发送同步和异步HTTTP请求/ p>
ASIFormDataRequest * request = [[[ASIFormDataRequest alloc] initWithURL:@http://someSite.com] autorelease];
[request setPostValue:@myValue1forKey:@myFormField1];
[request setPostValue:@myValue2forKey:@myFormField2];
//等
[request start];
NSError * error = [request error];
if(!error)
NSString * response = [request responseString];
他的框架是一个巨大的节省时间。
I am sending a post using 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]];
Within the body of the Request I am encoding the following NVP:
returnURL=http://someSite.com
The post is going through successfully, but I get an error back. The error states an invalid url.
So, I changed it to:
returnURL=http:%2F%2FsomeSite.com
With the same error.
I have also double checked it with Todd Ditchendorf's HTTP Client, with the same results.
I know I must be encoding this wrong can someone tell me what I am doing wrong?
Thanks Corey
**UPDATE: Thanks to those who answered. I missed an ampersand in my NVP values. So of course, as soon as I fixed it, everything was fine. What I suspectede, the encoding of a url in the body of the post was incorrect. I am not using the ASIHTTPRequest framework, but it did help me troubleshoot the problem (overkill for what I needed). **
Consider using Ben Copsey's ASIHTTPRequest
framework for generating and sending synchronous and asynchronous HTTTP requests (POST and 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];
His framework is a huge time-saver.
这篇关于NSURLRequest - 编码NSURLRequest的URL url POST Body(iPhone objective-C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!