NSURLRequest 中的转义字符

发布于 2024-09-16 05:48:28 字数 617 浏览 10 评论 0原文

我正在尝试在 iPhone 上使用 HTTP POST 将请求传递到 URL。 HTTP 正文包含一些转义字符。

NSString *requestMessage=[NSString stringWithString:@"?username/u001password/u001description"];
    NSMutableURLRequest *url=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://welcome.com"]];
    [url setHTTPMethod:@"POST"];
    [url setHTTPBody:[requestMessage dataUsingEncoding:NSUTF8StringEncoding]];
     NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:url delegate:self];

这里的转义字符是/u001

使用此代码我没有得到任何正确的响应。我认为问题仅在于转义字符。请给我一个解决方案,如何在 Cocoa 中给出这样的转义序列。提前致谢。

I'm trying to pass a request to a URL using HTTP POST on iPhone. The HTTP body contains some escape characters.

NSString *requestMessage=[NSString stringWithString:@"?username/u001password/u001description"];
    NSMutableURLRequest *url=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://welcome.com"]];
    [url setHTTPMethod:@"POST"];
    [url setHTTPBody:[requestMessage dataUsingEncoding:NSUTF8StringEncoding]];
     NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:url delegate:self];

The escape character here is /u001.

Using this code I don't get any correct responses. I think the trouble is with the escape characters only. Please give me a solution for how to give an escape sequence like this in Cocoa. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万劫不复 2024-09-23 05:48:28

您混淆了正斜杠 (/) 和反斜杠 (\)。您需要一个反斜杠来形成转义序列;斜杠就是斜杠,“/u001”就是斜杠、字母 u、两个数字 0 和一个数字 1。

也就是说,如果您确实想在字符串中包含 U+0001,即使 \u001 也是错误的。你想要 \x01 或者可能 \u0001 (但我似乎记得如果你使用 \u 来表示低于 U+ 的字符,GCC 会抱怨0100)。

我确实想知道为什么服务器需要 U+0001 作为分隔符。是否有适用于您查询的任何服务器的公共 API 文档?

You've confused forward slashes (/) with backslashes (\). You need a backslash to form an escape sequence; a slash is just a slash, and “/u001” is just a slash, the letter u, two digits zero, and a digit one.

That said, if you actually want to include U+0001 in your string, even \u001 is wrong. You want \x01 or maybe \u0001 (but I seem to remember that GCC complains if you use \u for a character lower than U+0100).

I do wonder why the server would require U+0001 as the separator, though. Are there public API docs for whatever server you're querying?

甜尕妞 2024-09-23 05:48:28

你想逃避什么?我不太明白你想做什么。你想写“&”吗?然后就去做吧。它不是 HTML。

除此之外,[NSString stringWithString:@"…constant string…"] 是超流体的。 @"...常量字符串..." 就是您所需要的。

NSString 中有一个方法可以向 URL 添加百分比转义符:-(void)stringByAddingPercentEscapesUsingEncoding:。也许这就是您正在寻找的?

What do you want to escape? I don't quite understand what you are trying to do. Do you want to write "&"? Then do it. It's not HTML.

Besides that, [NSString stringWithString:@"…constant string…"] is superfluid. @"…constant string…" is all you need.

There is a method in NSString to add percent-escapes to URLs: -(void)stringByAddingPercentEscapesUsingEncoding:. Maybe that's what you're looking for?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文