您的位置:首页 > 产品设计 > UI/UE

NSMutableURLRequest post提交参数

2016-03-01 09:52 525 查看
NSMutableURLRequest提交参数这个有点坑爹,有一天我在ping++ 源码里面找到了这些,我们首先来看NSMutableURLRequest不带参数的提交请求,我们来看看微信里面有一个方法

+ (NSString *)jumpToBizPay {

//============================================================
// V3&V4支付流程实现
// 注意:参数配置请查看服务器端Demo
// 更新时间:2015年11月20日
//============================================================
NSString *urlString = @"http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=ios";
//解析服务端返回json数据
NSError *error;
//加载一个NSURL对象
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
//将请求的url数据放到NSData对象中
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
if ( response != nil) {
NSMutableDictionary *dict = NULL;
//IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
dict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];

NSLog(@"url:%@",urlString);
if(dict != nil){
NSMutableString *retcode = [dict objectForKey:@"retcode"];
if (retcode.intValue == 0){
NSMutableString *stamp = [dict objectForKey:@"timestamp"];

//调起微信支付
PayReq* req = [[[PayReq alloc] init]autorelease];
req.partnerId = [dict objectForKey:@"partnerid"];
req.prepayId = [dict objectForKey:@"prepayid"];
req.nonceStr = [dict objectForKey:@"noncestr"];
req.timeStamp = stamp.intValue;
req.package = [dict objectForKey:@"package"];
req.sign = [dict objectForKey:@"sign"];
[WXApi sendReq:req];
//日志输出
NSLog(@"appid=%@\npartid=%@\nprepayid=%@\nnoncestr=%@\ntimestamp=%ld\npackage=%@\nsign=%@",[dict objectForKey:@"appid"],req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,req.sign );
return @"";
}else{
return [dict objectForKey:@"retmsg"];
}
}else{
return @"服务器返回错误,未获取到json对象";
}
}else{
return @"服务器返回错误";
}
}


这里面的方法是没有提交参数的, 如果要提交参数,我之前在网上找了很多,但是最后只能呵呵一下了!

下面是ping++里面的代码,请忽略没用的部分,我只给出方法 

NSURLConnection有同步和异步的方法,看你的需求咯!

- (void)normalPayAction:(id)sender
{
NSInteger tag = ((UIButton*)sender).tag;
if (tag == 1) {
self.channel = @"wx";
[self normalPayAction:nil];
} else if (tag == 2) {
self.channel = @"alipay";
} else if (tag == 3) {
self.channel = @"upacp";
} else if (tag == 4) {
self.channel = @"bfb";
} else {
return;
}

[mTextField resignFirstResponder];
long long amount = [[self.mTextField.text stringByReplacingOccurrencesOfString:@"." withString:@""] longLongValue];
if (amount == 0) {
return;
}
NSString *amountStr = [NSString stringWithFormat:@"%lld", amount];
NSURL* url = [NSURL URLWithString:kUrl];
NSMutableURLRequest * postRequest=[NSMutableURLRequest requestWithURL:url];

NSDictionary* dict = @{
@"channel" : self.channel,
@"amount" : amountStr
};
NSData* data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
NSString *bodyData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

[postRequest setHTTPBody:[NSData dataWithBytes:[bodyData UTF8String] length:strlen([bodyData UTF8String])]];
[postRequest setHTTPMethod:@"POST"];
[postRequest setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

ViewController * __weak weakSelf = self;
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[self showAlertWait];
[NSURLConnection sendAsynchronousRequest:postRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
dispatch_async(dispatch_get_main_queue(), ^{
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
[weakSelf hideAlert];
if (httpResponse.statusCode != 200) {
NSLog(@"statusCode=%ld error = %@", (long)httpResponse.statusCode, connectionError);
[weakSelf showAlertMessage:kErrorNet];
return;
}
if (connectionError != nil) {
NSLog(@"error = %@", connectionError);
[weakSelf showAlertMessage:kErrorNet];
return;
}
NSString* charge = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"charge = %@", charge);
[Pingpp createPayment:charge viewController:weakSelf appURLScheme:kUrlScheme withCompletion:^(NSString *result, PingppError *error) {
NSLog(@"completion block: %@", result);
if (error == nil) {
NSLog(@"PingppError is nil");
} else {
NSLog(@"PingppError: code=%lu msg=%@", (unsigned long)error.code, [error getMsg]);
}
[weakSelf showAlertMessage:result];
}];
});
}];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息