爲什麼NSURLSession
返回此響應任何想法:NSURLSession會比瀏覽器返回不同的響應,爲什麼?
您提供的授權類型不支持。只有基本和 的OAuth支持
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()
瀏覽器返回有意義的圖像信息不同的反應。我正在使用基本身份驗證來發送請求。
可能的[cURL to C#webrequest]重複(http://stackoverflow.com/questions/9421061/curl-to-c-sharp-webrequest) –