我正在尝试在我的手表套件应用程序中调用安全适配器。但我的请求失败,并在Xcode的输出窗口中显示以下错误消息。
Request url is http://:10080/UProject/adapters/MyAdapter/getMyBalance
2015-12-28 21:49:29.222 My WatchKit 1 Extension[59266:1304116] [DEBUG] [WL_AFHTTPRequestOperationManagerWrapper_PACKAGE] -[WLAFHTTPRequestOperationManagerWrapper start] in WLAFHTTPRequestOperationManagerWrapper.m:320 :: Starting the request with URL http://:10080/UProject/adapters/MyAdapter/getMyBalance?param=700015005
2015-12-28 21:49:29.222 My WatchKit 1 Extension[59266:1304116] After adapter Request
2015-12-28 21:49:29.247 My WatchKit 1 Extension[59266:1304116] [DEBUG] [WL_AFHTTPRequestOperationManagerWrapper_PACKAGE] -[WLAFHTTPRequestOperationManagerWrapper requestFailed:error:] in WLAFHTTPRequestOperationManagerWrapper.m:352 :: Request Failed
2015-12-28 21:49:29.247 My WatchKit 1 Extension[59266:1304116] [DEBUG] [WL_AFHTTPRequestOperationManagerWrapper_PACKAGE] -[WLAFHTTPRequestOperationManagerWrapper requestFailed:error:] in WLAFHTTPRequestOperationManagerWrapper.m:353 :: Response Status Code : 401
2015-12-28 21:49:29.248 My WatchKit 1 Extension[59266:1304116] [DEBUG] [WL_AFHTTPRequestOperationManagerWrapper_PACKAGE] -[WLAFHTTPRequestOperationManagerWrapper requestFailed:error:] in WLAFHTTPRequestOperationManagerWrapper.m:354 :: Response Error : Request failed: unauthorized (401)
//WKInterfaceController
import WatchKit
import Foundation
//import IBMMobileFirstPlatformFoundation
class InterfaceController: WKInterfaceController {
@IBOutlet var btnLoadFeeds: WKInterfaceButton!
@IBOutlet var btnConnect: WKInterfaceButton!
var challengeHandler : ChallengeHandler?
@IBOutlet var feedsTable: WKInterfaceTable!
var feeds = []
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Configure interface objects here.
btnLoadFeeds.setEnabled(false)
let connectListener = MyConnectListener()
WLClient.sharedInstance().registerChallengeHandler(MyChallengeHandler(vc: self))
WLClient.sharedInstance().wlConnectWithDelegate(connectListener)
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
@IBAction func btnConnectClicked() {
let url : NSURL = NSURL(string: "/adapters/SecureAdapter/getBalance")!
let request : WLResourceRequest = WLResourceRequest(URL: url, method: WLHttpMethodGet)
request.setQueryParameterValue("700015005", forName: "param")
request.sendWithCompletionHandler {
(response: WLResponse!, error: NSError!) -> Void in
if error != nil
{
NSLog("Adapter invocation failure. Error: %@", error.description)
} else
{
NSLog(response.responseText)
}
}
NSLog("After adapter Request")
}
@IBAction func btnLoadClicked() {
btnConnect.setHidden(true)
btnLoadFeeds.setHidden(true)
if (feeds.count > 0 )
{
feedsTable.setNumberOfRows(feeds.count, withRowType: "FeedRowController")
for (index,feedItem) in feeds.enumerate()
{
let row = feedsTable.rowControllerAtIndex(index) as! FeedRowController
row.labelText.setText(feedItem["title"] as? String)
print(feedItem)
}
}
}
func onFailure(response: WLFailResponse!) {
print(" failure")
}
func onSuccess(response: WLResponse!) {
print("success")
print(response.responseText)
feeds = response.getResponseJson()["Items"] as! NSArray
}
}
// MyChallengeHandler
进口基金会
类MyChallengeHandler:ChallengeHandler {
var challengeHandler : ChallengeHandler?
let vc : InterfaceController
init(vc: InterfaceController){
self.vc = vc
super.init(realm: "LDAPRealm")
}
override func isCustomResponse(response: WLResponse!) -> Bool {
if response != nil && response.responseText != nil{
if response.responseText.lowercaseString.rangeOfString("j_security_check") != nil{
NSLog("Detected j_security_check string - returns true")
return true
}
}
return false
}
override func handleChallenge(response: WLResponse!) {
NSLog("handleChallenge")
vc.challengeHandler = self
dispatch_async(dispatch_get_main_queue()) {
self.challengeHandler?.submitLoginForm("j_security_check", requestParameters: ["j_username": "demouser", "j_password": "12345678"], requestHeaders: nil, requestTimeoutInMilliSeconds:0, requestMethod: "POST")
}
}
override func onSuccess(response: WLResponse!) {
NSLog("zaq3 Challenge succeeded")
self.submitSuccess(response)
}
override func onFailure(response: WLFailResponse!) {
NSLog(" zaq3 Challenge failed")
self.submitFailure(response)
}
}
任何人都可以指出代码中缺少的内容。
答案 0 :(得分:0)
我修改了handleChallenge self.submitLoginForm(“j_security_check”,requestParameters:[“j_username”:“demouser”,“j_password”:“12345678”],requestHeaders:nil,requestTimeoutInMilliSeconds:0,requestMethod:“POST “)