ios – 如何在Swift中进行Http get并设置httpHeader?

我尝试在Objective-C中创建HTTP Get.

它使用NSMutableuRLRequest *请求;在Objective-C中,使用[request setHTTPMethod:@“GET”];选择GET或POST.

并通过以下代码设置标头:

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

我引用链接How to make an HTTP request in Swift?,它只显示以下代码

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))
}

但它没有选择Get或Post,也没有设置标题.
如何判断Http方法是Get还是Post?以及如何设置标题?在斯威夫特

提前致谢.

解决方法

request.HTTPMethod = "POST"
request.addValue("application/json",forHTTPHeaderField: "Content-Type")
request.addValue("application/json",forHTTPHeaderField: "Accept")

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...