美文网首页
Http 设置header

Http 设置header

作者: 小木___Boy | 来源:发表于2015-12-10 15:46 被阅读813次

    在做HTTP或者HTTPS 数据请求的时候,有些设备信息可以添加到http的header里面。

    1、原生数据请求

      NSString *strUrl=[NSString stringWithFormat:@" 你的地址"];
      NSURL *url=[NSURL URLWithString:strUrl];
      NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20];
      [request setHTTPMethod:@"GET"];[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
      [request setValue:@"字段对应的值1" forHTTPHeaderField:@"你头部的字段1"];
      [request setValue:@"字段对应的值2" forHTTPHeaderField:@"你头部的字段2"];
      mConn=[[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:true];
    

    2、使用AFNetworking2.x

      AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
      manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
      manager.requestSerializer = [AFJSONRequestSerializer serializer];
      manager.responseSerializer = [AFJSONResponseSerializer serializer];
      [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
      [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
    

    相关文章

      网友评论

          本文标题:Http 设置header

          本文链接:https://www.haomeiwen.com/subject/pflyhttx.html