gpt4 book ai didi

ios - 适用于iOS中HTTP POST请求的正确JSON编码

转载 作者:行者123 更新时间:2023-11-29 03:04:56 24 4
gpt4 key购买 nike

先前发布了有关JSON解析无法正常运行的查询。是否使用数据包嗅探器以及其他运行正常的客户端进行了更多调查,发现这是语法问题,但我似乎仍然无法解决。

底部的代码使HTTP请求中包含JSON,如下所示:
{“核心价值”}

我的服务器实际上正在使用以下语法查找JSON:
键=%22value%22

我尝试编写一些手动执行此操作的代码,但认为iOS必须有一些开箱即用的功能,而且我也不想在将来出现问题。

我把它弄乱了一段时间,试图找到适合该工作的代码,但是没找到(您可以看到一些我尝试注释掉的代码)。谁能帮我?

+ (NSString*)makePostCall:(NSString*)urlSuffix
keys:(NSArray*)keys
objects:(NSArray*)objects{
NSDictionary *params = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
// NSString *dataString = [self getDataStringFromDictionary:params];
// NSData *jsonData = [dataString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params
options:0
error:&error];

// id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
// NSLog(@"%@", jsonObject);

if (!jsonData) {
// should not happen
NSError *error;
NSLog(@"Got an error parsing the parameters: %@", error);

return nil;
} else {
// NSString *jsonRequest = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
// NSLog(@"%@", jsonRequest);

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", urlPrefix, urlSuffix]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];


// NSData *requestData = [jsonRequest dataUsingEncoding:NSUTF8StringEncoding];


[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
// [request setValue:@"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody: jsonData];

NSURLResponse * response = nil;
NSError * error = nil;

NSData * data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];

// TODO: handle error somehow

NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

return returnString;
}
}

最佳答案

你说:


  发生的情况是服务器期望的每个参数的内容实际上是JSON,但是每个参数都是单独发送的


如果您要发布的是标准application/x-www-form-urlencoded,但想要对值进行JSON编码,则这有点不寻常(我只使用JSON或x-www-form-urlencoded,但不将它们组合在一起),但并非如此难做。如您的示例中所示,关键问题是必须百分比转义所得的JSON。例如:

NSURL *url = [NSURL URLWithString:kURLString];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];

// information to be encoded for first parameter

NSDictionary *value1 = @{@"name" : @"Dwayne Johnson",
@"age" : @41,
@"sex" : @"male"};
NSError *error;
NSData *json1 = [NSJSONSerialization dataWithJSONObject:value1 options:0 error:&error];
NSAssert(json1, @"Unable to create JSON for value1: %@", error);

// information to be encoded for second parameter

NSDictionary *value2 = @{@"name" : @"Lauren Hashian",
@"age" : @29,
@"sex" : @"female"};

NSData *json2 = [NSJSONSerialization dataWithJSONObject:value2 options:0 error:&error];
NSAssert(json2, @"Unable to create JSON for value1: %@", error);

// combine these parameters into single dictionary

NSDictionary *parameters = @{@"person" : json1, @"partner" : json2};
NSData *requestData = [self encodePostParameters:parameters];

[request setHTTPBody:requestData];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (!data) {
NSLog(@"sendAsynchronousRequest failed: %@", connectionError);
return;
}

NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);

// you can now use that `responseString` variable (but do this here,
// inside this completion block!)
}];


这将导致请求 HTTPBody

person=%7B%22age%22%3A41%2C%22name%22%3A%22Dwayne+Johnson%22%2C%22sex%22%3A%22male%22%7D&partner=%7B%22age%22%3A29%2C%22name%22%3A%22Lauren+Hashian%22%2C%22sex%22%3A%22female%22%7D


这里有趣的逻辑是转义百分比,我在称为 percentEscapeString的方法中进行此操作,该方法从 encodePostParameters调用,定义如下:

/** Percent escape the post parameters for x-www-form-urlencoded request
*
* @param parameters A NSDictionary of the parameters to be encoded
*
* @return The NSData to be used in body of POST request (or URL or GET request)
*/
- (NSData *)encodePostParameters:(NSDictionary *)parameters
{
NSMutableArray *results = [NSMutableArray array];

for (NSString *key in parameters) {
NSString *string;
id value = parameters[key];
if ([value isKindOfClass:[NSData class]]) {
string = [[NSString alloc] initWithData:value encoding:NSUTF8StringEncoding];
} else if ([value isKindOfClass:[NSString class]]) {
string = value;
} else { // if you want to handle other data types, add that here
string = [value description];
}
[results addObject:[NSString stringWithFormat:@"%@=%@", key, [self percentEscapeString:string]]];
}

NSString *postString = [results componentsJoinedByString:@"&"];

return [postString dataUsingEncoding:NSUTF8StringEncoding];
}

/** Percent escape string
*
* Note, this does not use `stringByAddingPercentEscapesUsingEncoding`, because
* that that will not percent escape some critical reserved characters (notably
* `&` and `+`). This uses `CFURLCreateStringByAddingPercentEscapes`, instead.
*/
- (NSString *)percentEscapeString:(NSString *)string
{
NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)string,
(CFStringRef)@" ",
(CFStringRef)@":/?@!$&'()*+,;=",
kCFStringEncodingUTF8));
return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"];
}

关于ios - 适用于iOS中HTTP POST请求的正确JSON编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22884571/

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