NSURLSession returns 响应与浏览器不同,为什么?
NSURLSession returns different response than browser would, why?
知道为什么 NSURLSession
returns 这个回复:
The authorization type you provided is not supported. Only Basic and
OAuth are supported
let username = "8r6nibK8Pe28WFhjTX1gGb1Rhhhc7xM02v7XKRPedL4"
let password = "8r6nibK8Pe28WFhjTX1gGb1Rhhhc7xM02v7XKRPedL4"
let loginString = NSString(format: "%@:%@", username, password)
let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!
let base64LoginString = loginData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let authString = "Basic \(base64LoginString)"
config.HTTPAdditionalHeaders = ["Authorization" : authString]
let url = NSURL(string: "https://api.datamarket.azure.com/Bing/Search/Image?$format=json&Query='Rambo'+'Oscar'")
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
let a = NSURLSession(configuration: config).dataTaskWithRequest(request, completionHandler: {data, urlResponse, error in
if data != nil {
let str = NSString(data: data!, encoding: NSUTF8StringEncoding)
print(str)
} else {
_ = 7
}
})
a.resume()
浏览器 returns 对有意义的图像信息的不同响应。我正在使用基本身份验证来发送请求。
问题可能是选项:.Encoding64CharacterLineLength
。尝试:
let base64LoginString = loginData.base64EncodedStringWithOptions([])
要进行调查,请使用 Charles Proxy 查看请求中的差异。 – zaph 10 分钟前
知道为什么 NSURLSession
returns 这个回复:
The authorization type you provided is not supported. Only Basic and OAuth are supported
let username = "8r6nibK8Pe28WFhjTX1gGb1Rhhhc7xM02v7XKRPedL4"
let password = "8r6nibK8Pe28WFhjTX1gGb1Rhhhc7xM02v7XKRPedL4"
let loginString = NSString(format: "%@:%@", username, password)
let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!
let base64LoginString = loginData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let authString = "Basic \(base64LoginString)"
config.HTTPAdditionalHeaders = ["Authorization" : authString]
let url = NSURL(string: "https://api.datamarket.azure.com/Bing/Search/Image?$format=json&Query='Rambo'+'Oscar'")
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
let a = NSURLSession(configuration: config).dataTaskWithRequest(request, completionHandler: {data, urlResponse, error in
if data != nil {
let str = NSString(data: data!, encoding: NSUTF8StringEncoding)
print(str)
} else {
_ = 7
}
})
a.resume()
浏览器 returns 对有意义的图像信息的不同响应。我正在使用基本身份验证来发送请求。
问题可能是选项:.Encoding64CharacterLineLength
。尝试:
let base64LoginString = loginData.base64EncodedStringWithOptions([])
要进行调查,请使用 Charles Proxy 查看请求中的差异。 – zaph 10 分钟前