How to make Http-get and install httpHeader in Swift?

I am trying to do HTTP Get in Objective-C.

Used by NSMutableURLRequest *request; in Objective-C and use [request setHTTPMethod:@"GET"]; to select GET or POST .

And set the header through the following code:

 [request setValue:@"Application/json" forHTTPHeaderField:@"Accept"]; [request addValue:@"Application/json" forHTTPHeaderField:@"Content-Type"]; [request addValue:[NSString stringWithFormat:@"Basic %@",USER_PWD] forHTTPHeaderField:@"Authorization"]; 

I refer to the link How to make an HTTP request in Swift? , and it only displays the following code:

 let url = NSURL(string: "http://www.stackoverflow.com") let request = NSURLRequest(URL: url) NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in println(NSString(data: data, encoding: NSUTF8StringEncoding)) } 

But he did not select GET or POST , nor did he set the header. How to solve the Http GET or POST method? and how to set the title? in swift

Thanks in advance.

+5
source share
1 answer
 request.HTTPMethod = "POST" request.addValue("application/json", forHTTPHeaderField: "Content-Type") request.addValue("application/json", forHTTPHeaderField: "Accept") 
+12
source

All Articles