91 POST JSON 多值参数 获得mineType

1>POST JSON
有时候服务器接收的是json数据,这个时候需要将json数据传给服务器:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 1.创建请求
    NSURL *url = [NSURL URLWithString:@"http://192.168.1.200:8080/MJServer/order"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";

    // 2.设置请求头
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    // 3.设置请求体
    NSDictionary *json = @{
                           @"order_id" : @"123",
                           @"user_id" : @"789",
                           @"shop" : @"Toll"
                           };

//    NSData --> NSDictionary
    // NSDictionary --> NSData
    NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
    request.HTTPBody = data;

    // 4.发送请求
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSLog(@"%d", data.length);
    }];
}
需要注意的是POST JSON的Content-Length可以不用传(自己不传,系统会传)
而文件上传的时候Content-Type必须要传

如果仅仅是传一般参数例如username给服务器的时候,POST的是string的data
2>多值参数

有时候一个url的参数可以接收很多参数值,例如天气预报的可以传多个地名给服务器
服务器给据每个地名给出天气预报,例如place=南京&place=北京,这个时候服务器是用数组接收参数值!
3>获得一种文件的mineType(Content-Type)
    // 给本地文件发送一个请求
    NSURL *fileurl = [[NSBundle mainBundle] URLForResource:@"itcast.txt" withExtension:nil];
    NSURLRequest *request = [NSURLRequest requestWithURL:fileurl];
    NSURLResponse *repsonse = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&repsonse error:nil];

    // 得到mimeType
    NSLog(@"%@", repsonse.MIMEType);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值