zoukankan      html  css  js  c++  java
  • 传送表单

    //参数1名字=参数1数据&参数2名字=参数2数据&参数3名字=参数3数据&...
       // NSString *post = [NSString stringWithFormat:@"active=login&u_name=a&u_pwd=000618"];
        //将NSSrring格式的参数转换格式为NSData,POST提交必须用NSData数据。
        NSData *postData = [postString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
        //计算POST提交数据的长度
        NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
        NSLog(@"postLength=%@",postLength);
        //定义NSMutableURLRequest
        //NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
        //设置提交目的url
        //[request setURL:[NSURL URLWithString:serviceURL]];
        
        NSURL *URL=[NSURL URLWithString:serviceURL];
        NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:3];
        //设置提交方式为 POST
        [theRequest setHTTPMethod:@"POST"];
        //设置http-header:Content-Type
        //这里设置为 application/x-www-form-urlencoded ,如果设置为其它的,比如text/html;charset=utf-8,或者 text/html 等,都会出错。不知道什么原因。
        [theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        //设置http-header:Content-Length
        [theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
        //设置需要post提交的内容
        [theRequest setHTTPBody:postData];
        
        //定义
        NSHTTPURLResponse* urlResponse = nil;
        NSError *error = [[NSError alloc] init];
        //同步提交:POST提交并等待返回值(同步),返回值是NSData类型。
        NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&urlResponse error:&error];
        //将NSData类型的返回值转换成NSString类型
        NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        NSObject *returnObj=[result JSONValue];
        return returnObj;
    
  • 相关阅读:
    重学Docker
    rabbitmq基本知识了解
    rabbitmq之mq协议了解
    微信公众平台开发教程(三) 基础框架搭建
    微信公众平台开发教程(二) 基本原理及消息接口
    微信公众平台开发教程(一) 微信公众账号注册流程
    微信公众平台开发教程(十) 订阅号与服务号的区别
    vue cli3.0快速搭建项目详解(强烈推荐)
    vue 3.0 初体验 (项目搭建)
    手把手教你vue3.0项目搭建
  • 原文地址:https://www.cnblogs.com/jiackyan/p/3388101.html
Copyright © 2011-2022 走看看