iOS 原生請求

小大發表於2017-12-21

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];//開始請求

相關文章