iOS]使用form-data的方式提交表单

Posted itlover2013

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS]使用form-data的方式提交表单相关的知识,希望对你有一定的参考价值。

https://blog.csdn.net/wsyx768/article/details/103686724

 

做内购支付时,后端需要验证订单支付状态,因为这个校验字符串非常长,所以使用form-data的方式提交表单。

 

#pragma mark - 内购支付成功后确认

- (void)netApplePayWithTran:(SKPaymentTransaction *)transaction OrderId:(NSString *)orderId {
NSString *tranID = transaction.transactionIdentifier;
[[XYStore defaultStore] base64Receipt:^(NSString *base64Data) {
// base64Data = [GAPublicClass URLEncodedString:base64Data];
NSMutableDictionary *parms = [NSMutableDictionary dictionary];
[parms setObject:tranID forKey:@"transactionId"]; // 苹果内购交易ID
[parms setObject:base64Data forKey:@"payload"]; // 校验体(base64字符串
[parms setObject:orderId forKey:@"orderId"]; // 订单id
[self validationPartPost:parms];
} failure:^(NSError *error) {

}];
}

- (void)validationPartPost:(NSDictionary *)dicData {
NSString *POST_BOUNDS = @"yxqipayanzheng";
NSString *tokenStr = [AccountTool sqLoginModel].token;

NSURL *url = [NSURL URLWithString:[API_DOMAIN_COMMUNITY stringByAppendingString:url_apple_pay]];
NSMutableString *bodyContent = [NSMutableString string];
for(NSString *key in dicData.allKeys){
id value = [dicData objectForKey:key];
[bodyContent appendFormat:@"--%@\\r\\n",POST_BOUNDS];
[bodyContent appendFormat:@"Content-Disposition: form-data; name=\\"%@\\"\\r\\n\\r\\n",key];
[bodyContent appendFormat:@"%@\\r\\n",value];
}
[bodyContent appendFormat:@"--%@--\\r\\n",POST_BOUNDS];
NSData *bodyData=[bodyContent dataUsingEncoding:NSUTF8StringEncoding];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];
[request addValue:[NSString stringWithFormat:@"multipart/form-data;boundary=%@",POST_BOUNDS] forHTTPHeaderField:@"Content-Type"];
[request setValue:tokenStr forHTTPHeaderField:@"token"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:bodyData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:KWindow animated:YES];
});
NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSInteger status = [responseJSON[@"status"] integerValue];
if (status == 200) {

}
}];
[task resume];
}
TO
————————————————
版权声明:本文为CSDN博主「风浅月明」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wsyx768/article/details/103686724

以上是关于iOS]使用form-data的方式提交表单的主要内容,如果未能解决你的问题,请参考以下文章

iOS AFN POST  提交表单后台接收不到

html中表单使用post方式提交,为啥没有值

form表单提交file文件传输失败

如何在Controller获取以multipart/form-data方式提交表单的参数值

from表单提交---name值没有传递到后台原因解析

表单提交原理