gpt4 book ai didi

ios - 是否有 'trick' 用于在 NSURLSession 连接中发送原始 NSString 而不是 NSData?

转载 作者:行者123 更新时间:2023-11-29 10:43:48 25 4
gpt4 key购买 nike

我正在尝试使用 NSURLSession 与后端进行通信,但是,后端未设置为正确读取 NSData,因此它将我发送的 NSData 登录详细信息视为错误,我想知道是否可以获取NSURLSession 发送原始字符串而不是 NSData 对象。我查阅了书籍和网络,但我已经困惑了几个星期。

重做后端不是一种选择,负责这件事的工程师离开了。欢迎任何帮助。

谢谢。这是我到目前为止所做的,以防有人需要查看一些代码。

NSError *error;

NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration ];

NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];

NSString *rawUrlString = @"backend_url";

NSURL *url = [NSURL URLWithString:rawUrlString];

NSLog(@"%@", url);


NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
login data here

nil];

//NSLog(@"%@", parameters);

NSData *rawJson = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&error];

NSString *myString = [[NSString alloc] initWithData:rawJson encoding:NSUTF8StringEncoding];

NSData *finalData = [myString dataUsingEncoding:NSUTF8StringEncoding];


NSMutableData *body = [NSMutableData data];

[body appendData:finalData];

[request setHTTPMethod:@"POST"];
[request setHTTPBody:body];

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"charset"];
[request setValue:@"XMLHttpRequest" forHTTPHeaderField:@"X-Requested-With"];

NSURLSessionDataTask *postTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]);

}];

[postTask resume];

`

更新:我已经清理了不必要的位,这是最终代码(虽然仍然不起作用)

 NSError *error;

NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration ];

NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];

NSString *rawUrlString = @"backend_url";

NSURL *url = [NSURL URLWithString:rawUrlString];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:login details nil];


NSData *rawJson = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&error];

[request setHTTPMethod:@"POST"];
[request setHTTPBody:rawJson];

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

NSURLSessionDataTask *postTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]);

}];

[postTask resume];

最佳答案

您的后端已设置为处理 NSData。通过 Internet 连接发送字符串是不可能的,您只能发送 NSData 并且所有服务器都需要它。

这里的代码是错误的:

NSData *rawJson = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&error];

NSString *myString = [[NSString alloc] initWithData:rawJson encoding:NSUTF8StringEncoding];

NSData *finalData = [myString dataUsingEncoding:NSUTF8StringEncoding];

只需这样做:

NSData *finalData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&error];

NSData 对象将包含一个格式正确的字符串,以便服务器也将其识别为字符串。请注意,它将采用 UTF-8 编码,也许在某些服务器上您需要将其更改为其他编码。

关于ios - 是否有 'trick' 用于在 NSURLSession 连接中发送原始 NSString 而不是 NSData?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23187571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com