如何在 NSURLRequest 中使用身份验证令牌

发布于 2024-12-23 21:57:35 字数 118 浏览 6 评论 0原文

我正在使用带有 REST API 的服务,它为我提供身份验证令牌作为登录 POST 请求的响应。

现在,为了对每个请求进行身份验证,我需要随请求发送该令牌。 如何在我发送到服务器的每个请求中使用此令牌???

I'm using a service with REST API, which provides me an authentication token as the response for the sign in POST request.

Now for authentication on every request i need to send that token with the request.
How to use this token with every request i send to server ???

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

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

发布评论

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

评论(1

梨涡少年 2024-12-30 21:57:36

我不完全确定你的问题需要什么,但这是一个猜测。

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://rest.u.rl/do/this"];
NSString *header = [NSString stringWithFormat:@"%@",token]; //format as needed
[request setValue:header forHTTPHeaderField:@"TokenFieldName"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPMethod: @"POST"];

要对每个其他执行此操作,请执行 if (i % 2 == 0) 或类似的操作。

I'm not totally sure what you need from your question, but here is a guess.

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://rest.u.rl/do/this"];
NSString *header = [NSString stringWithFormat:@"%@",token]; //format as needed
[request setValue:header forHTTPHeaderField:@"TokenFieldName"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPMethod: @"POST"];

And to do this for every other one, do a if (i % 2 == 0) or something to that effect.

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