我有一个具有基本身份验证的RESTFull服务,我想从iOS + swift调用它。我必须如何以及在何处为此请求提供凭据?
我的代码(对不起,我刚刚开始学习iOS / obj-c / swift):
class APIProxy: NSObject {
var data: NSMutableData = NSMutableData()
func connectToWebApi() {
var urlPath = "http://xx.xx.xx.xx/BP3_0_32/ru/hs/testservis/somemethod"
NSLog("connection string \(urlPath)")
var url: NSURL = NSURL(string: urlPath)
var request = NSMutableURLRequest(URL: url)
let username = "hs"
let password = "1"
let loginString = NSString(format: "%@:%@", username, password)
let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)
let base64LoginString = loginData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.fromMask(0))
request.setValue(base64LoginString, forHTTPHeaderField: "Authorization")
var connection: NSURLConnection = NSURLConnection(request: request, delegate: self)
connection.start()
}
//NSURLConnection delegate method
func connection(connection: NSURLConnection!, didFailWithError error: NSError!) {
println("Failed with error:\(error.localizedDescription)")
}
//NSURLConnection delegate method
func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
//New request so we need to clear the data object
self.data = NSMutableData()
}
//NSURLConnection delegate method
func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
//Append incoming data
self.data.appendData(data)
}
//NSURLConnection delegate method
func connectionDidFinishLoading(connection: NSURLConnection!) {
NSLog("connectionDidFinishLoading");
}
}
您需要在URLRequest
实例中提供凭据,例如Swift 3:
let username = "user"
let password = "pass"
let loginString = String(format: "%@:%@", username, password)
let loginData = loginString.data(using: String.Encoding.utf8)!
let base64LoginString = loginData.base64EncodedString()
// create the request
let url = URL(string: "http://www.example.com/")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
// fire off the request
// make sure your class conforms to NSURLConnectionDelegate
let urlConnection = NSURLConnection(request: request, delegate: self)
或在NSMutableURLRequest
Swift 2中:
// set up the base64-encoded credentials
let username = "user"
let password = "pass"
let loginString = NSString(format: "%@:%@", username, password)
let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!
let base64LoginString = loginData.base64EncodedStringWithOptions([])
// create the request
let url = NSURL(string: "http://www.example.com/")
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
// fire off the request
// make sure your class conforms to NSURLConnectionDelegate
let urlConnection = NSURLConnection(request: request, delegate: self)
问题内容: 我必须 使用 HTTP Basic身份验证从http服务器下载和解析XML文件。现在,我这样做: 但是以这种方式,我无法从具有http身份验证的服务器获取xml(或者我只是根本不知道该文档)文档。 如果您能向我展示实现我的目标的最好,最简单的方法,我将不胜感激。 问题答案: 您可以使用。例如: 这将设置默认值,并将在 所有 请求中使用。显然,当您不需要所有请求的凭据或多个不同的凭据(可
问题内容: 我在iBooks中阅读了Apple的The Programming Language Swift ,但无法弄清楚如何在Swift中发出HTTP请求(例如cURL)。我需要导入Obj- C类还是只需要导入默认库?还是无法基于本机Swift代码发出HTTP请求? 问题答案: 您可以使用,而且还是因为你通常在Objective- C做。请注意,对于iOS 7.0和更高版本,它是首选。 使用
我在iBooks上读过Apple的编程语言Swift,但不知道如何在Swift中发出HTTP请求(类似于cURL)。我需要导入Obj-C类还是只需要导入默认库?或者不可能基于本机Swift代码发出HTTP请求?
我在iBooks中阅读了苹果公司的编程语言Swift,但不知道如何在Swift中发出HTTP请求(类似cURL的东西)。我需要导入Obj-C类还是只需要导入默认库?还是不能基于原生Swift代码进行HTTP请求?
问题内容: 我不确定如何发送HTTP Auth标头。 我有以下HttpClient来获取请求,但不确定如何发送请求? 问题答案: HttpClient 文档及其示例代码中对此进行了介绍。
问题内容: 我正在尝试使用 基本身份验证 访问URL 。 URL返回JSON数据。 如何在下面的http请求中添加用户名和密码? 问题答案: 请参阅https://angular.io/guide/http或https://v6.angular.io/guide/http#adding- headers 然后使用标题: