การส่งคำขอ POST
และ GET
ใน iOS นั้นค่อนข้างง่าย และไม่จำเป็นต้องมีกรอบงานเพิ่มเติม
POST
คำขอ:
เราเริ่มต้นด้วยการสร้าง POST
's body
(เช่น สิ่งที่เราต้องการส่ง) ของเราเป็น NSString
และแปลงเป็น NSData
objective-c
NSString *post = [NSString stringWithFormat:@"test=Message&this=isNotReal"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
ต่อไป เราจะอ่าน postData
's length
เพื่อที่เราจะได้ส่งต่อในคำขอได้
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
ตอนนี้เรามีสิ่งที่ต้องการโพสต์แล้ว เราสามารถสร้าง NSMutableURLRequest
และรวม postData
ของเราได้
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://YourURL.com/FakeURL"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
swift
let post = "test=Message&this=isNotReal"
let postData = post.data(using: String.Encoding.ascii, allowLossyConversion: true)
let postLength = String(postData!.count)
var request = URLRequest(url: URL(string: "http://YourURL.com/FakeURL/PARAMETERS")!)
request.httpMethod = "POST"
request.addValue(postLength, forHTTPHeaderField: "Content-Length")
request.httpBody = postData;
และสุดท้าย เราสามารถส่งคำขอของเรา และอ่านคำตอบโดยสร้าง NSURLSession
ใหม่:
objective-c
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"Request reply: %@", requestReply);
}] resume];
swift
let session = URLSession(configuration: .default)
session.dataTask(with: request) {data, response, error in
let requestReply = NSString(data: data!, encoding: String.Encoding.ascii.rawValue)
print("Request reply: \(requestReply!)")
}.resume()
GET
คำขอ:
ด้วยคำขอ GET
โดยพื้นฐานแล้วจะเป็นสิ่งเดียวกัน เพียง ไม่มี HTTPBody
และ Content-Length
objective-c
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://YourURL.com/FakeURL/PARAMETERS"]];
[request setHTTPMethod:@"GET"];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"Request reply: %@", requestReply);
}] resume];
swift
var request = URLRequest(url: URL(string: "http://YourURL.com/FakeURL/PARAMETERS")!)
request.httpMethod = "GET"
let session = URLSession(configuration: .default)
session.dataTask(with: request) {data, response, error in
let requestReply = NSString(data: data!, encoding: String.Encoding.ascii.rawValue)
print("Request reply: \(requestReply!)")
}.resume()
หมายเหตุด้านข้าง คุณสามารถเพิ่ม Content-Type
(และข้อมูลอื่นๆ) ได้โดยการเพิ่มสิ่งต่อไปนี้ใน NSMutableURLRequest
ของเรา เซิร์ฟเวอร์อาจจำเป็นต้องใช้สิ่งนี้เมื่อทำการร้องขอ เช่น json< /ก>.
objective-c
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
รหัสตอบกลับสามารถอ่านได้โดยใช้ [(NSHTTPURLResponse*)response statusCode]
swift
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
อัปเดต: sendSynchronousRequest
เลิกใช้แล้ว จาก ios9 และ osx-elcapitan (10.11) และออก <ส>
NSURLResponse *requestResponse; NSData *requestHandler = [NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:nil]; NSString *requestReply = [[NSString alloc] initWithBytes:[requestHandler bytes] length:[requestHandler length] encoding:NSASCIIStringEncoding]; NSLog(@"requestReply: %@", requestReply);
person
Aleksander Azizi
schedule
17.07.2012