1、swift带报文的网络请求
static func request_object(method:String,url:String,bodyStr:NSString, callback:(data:NSData!,resopnse:NSURLResponse!,error:NSError!)->Void){ let session = NSURLSession.sharedSession(); let request = NSMutableURLRequest(URL: NSURL(string: url)!); request.HTTPMethod = method; request.HTTPBody = bodyStr.dataUsingEncoding(NSUTF8StringEncoding); let task = session.dataTaskWithRequest(request, completionHandler: {(data,response,error) -> Void in callback(data: data, resopnse: response, error: error); } ) task.resume(); }
static func request_news_info(Params:Array<AnyObject>, callback:(data:NSData!,resopnse:NSURLResponse!,error:NSError!)->Void){ var params_dictionary = Dictionary<String,AnyObject>(); params_dictionary["Token"] = token; params_dictionary["Task"] = "1"; params_dictionary["Module"] = "7"; params_dictionary["Params"] = Params; do{ let data = try NSJSONSerialization.dataWithJSONObject(params_dictionary, options: NSJSONWritingOptions.PrettyPrinted) let strJson=NSString(data: data, encoding: NSUTF8StringEncoding) NetWork.request_object("POST", url: request_url, bodyStr: strJson!) { (data, resopnse, error) -> Void in callback(data: data, resopnse: resopnse, error: error) } }catch{ print("Error"); callback(data: nil, resopnse: nil, error: nil) } }
let array = Array<AnyObject>(); RequestData.request_news_info(array, callback: { (data, resopnse, error) -> Void in let string = NSString.init(data:data, encoding:NSUTF8StringEncoding); print(string); })
求大神