iOS 原生请求

235 阅读1分钟

NSMutableURLRequest*request=[[NSMutableURLRequestalloc]initWithURL:[NSURLURLWithString:url]];

request.HTTPMethod=@"POST";

NSString*postStr = [parametersobjectForKey:@"postString"];

request.HTTPBody=[postStr dataUsingEncoding:NSUTF8StringEncoding];

request.timeoutInterval=40;

//请求头

[request setValue:@"application/xml;charset=utf-8"forHTTPHeaderField:@"Content-Type"];

NSURLSession*session = [NSURLSessionsharedSession];

NSURLSessionDataTask*dataTask = [sessiondataTaskWithRequest:requestcompletionHandler:^(NSData*_Nullabledata,NSURLResponse*_Nullableresponse,NSError*_Nullableerror) {

             if(error) {

                   //请求失败

             }else{

                 //请求成功

             }

}];

[dataTaskresume];//开始请求