NSURLRequest - 为 NSURLRequest POST Body 编码 url (iPhone Objective-C)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/787582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:27:09  来源:igfitidea点击:

NSURLRequest - encode url for NSURLRequest POST Body (iPhone objective-C)

iphoneobjective-ccocoacocoa-touchhttp

提问by Corey Floyd

I am sending a post using NSURLRequest.

我正在使用 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:

在请求的正文中,我正在对以下 NVP 进行编码:

returnURL=http://someSite.com

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

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.

我还用 Todd Ditchendorf 的 HTTP Client 仔细检查了它,结果相同。

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). **

**更新:感谢那些回答。我在 NVP 值中遗漏了一个和号。所以当然,一旦我修复它,一切都很好。我怀疑,帖子正文中网址的编码不正确。我没有使用 ASIHTTPRequest 框架,但它确实帮助我解决了问题(对于我需要的东西来说太过分了)。**

回答by Alex Reynolds

Consider using Ben Copsey's ASIHTTPRequestframework for generating and sending synchronous and asynchronous HTTTP requests (POST and GET):

考虑使用 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];

His framework is a huge time-saver.

他的框架是一个巨大的节省时间。

回答by Chris Lundie

I don't know if this solves your problem, but

我不知道这是否能解决您的问题,但是

[parameterString length]

is often not be equal to the length of

通常不等于长度

[parameterString dataUsingEncoding:NSUTF8StringEncoding]

because UTF8 encodes characters with varying numbers of bytes.

因为 UTF8 编码具有不同字节数的字符。