【发布时间】:2015-01-13 19:16:55
【问题描述】:
我对 IOS 开发比较陌生。所以请多多包涵,原谅任何点头!
我正在尝试使用 NSURLConnection 从 SOAP Web 服务取回数据,它工作正常。
但是,一旦我将 URL 从 http 更改为 https,我就不会再获得任何数据,也不会收到错误或任何我期望的错误。
https 证书是来自 godaddy 的有效证书,它可以正确设置并在所有浏览器上正确查看等等,所以基本上任何人都可以指出正确的方向,为什么从 http 更改为 https 时什么都没有发生...... ..
代码是Swift,如下:
// Create the SOAP Message to send
var soapMessage = "<?xml version='1.0' encoding='UTF-8'?><SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:ns1='http://tempuri.org/'><SOAP-ENV:Body><ns1:get_Test/></SOAP-ENV:Body></SOAP-ENV:Envelope>"
NSLog("soapMessage generated is: %@", soapMessage)
// Soap URL Works
var urlString = "http://api.domain.com/testservice.svc"
//Below https URL does not work
//var urlString = "https://api.domain.com/testservice.svc"
var url = NSURL(string: urlString)
var theRequest = NSMutableURLRequest(URL: url!)
//Get Length of request
var msgLength = String(countElements(soapMessage))
// POST Header values
theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
theRequest.addValue(msgLength, forHTTPHeaderField: "Content-Length")
theRequest.addValue("http://tempuri.org/IService/get_Test", forHTTPHeaderField: "SoapAction")
theRequest.HTTPMethod = "POST"
theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
NSLog("Request is: %@", theRequest.allHTTPHeaderFields!)
var connection = NSURLConnection(request: theRequest, delegate: self, startImmediately: true)
connection?.start()
if (connection == true) {
var mutableData : Void = NSMutableData.initialize()
}
进一步的 NSURLConnection 代码是:
// NSURLConnectionDelegate
func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
mutableData.length = 0;
}
func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
mutableData.appendData(data)
}
func connection(connection: NSURLConnection, didFailWithError error: NSError) {
NSLog("Error with Soap call: %@", error)
}
func connectionDidFinishLoading(connection: NSURLConnection!) {
var xmlParser = NSXMLParser(data: mutableData)
xmlParser.delegate = self
xmlParser.parse()
xmlParser.shouldResolveExternalEntities = true
}
// NSURLConnectionDelegate
然后我有了解析器的东西,比如 didStartElement、didEndElement、foundCharacters、parserDidEndDocument 等。
我还添加了一些 NSURLConnection 的其他代表,如下所示,但没有任何改变。在日志显示时调用它们。
func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace?) -> Bool
{
NSLog("am here")
return protectionSpace?.authenticationMethod == NSURLAuthenticationMethodServerTrust
}
func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge?)
{
NSLog("am here 2")
if challenge?.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust
{
if challenge?.protectionSpace.host == "api.domain.com"
{
NSLog("yep")
let credentials = NSURLCredential(forTrust: challenge!.protectionSpace.serverTrust)
challenge!.sender.useCredential(credentials, forAuthenticationChallenge: challenge!)
}
}
challenge?.sender.continueWithoutCredentialForAuthenticationChallenge(challenge!)
}
它通过代码运行到“是”,因此据我了解应该信任证书,但当我使用 https url 时仍然没有显示任何内容。
所以除了https之外的任何代码都没有区别,并且证书是正确有效的,那么我怎样才能使用https而不是http来取回数据呢?
非常感谢 - 如果这是一个愚蠢的问题,我们深表歉意!
戴夫
补充:
好的,我现在已将连接更改为:
var connection = NSURLConnection(request: theRequest, delegate: self, startImmediately: false)
connection?.start()
if (connection == true) {
var mutableData : Void = NSMutableData.initialize()
} else {
NSLog("Error with connection, details: %@", connection!)
}
所以现在当我使用 SSL 运行它时,它会记录“连接错误”!
然后我将 didreceiveresponse 更改为:
func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
mutableData.length = 0;
var httpresponse = response as? NSHTTPURLResponse
println("status \(httpresponse?.statusCode)")
println("headers \(httpresponse?.allHeaderFields)")
}
我看到返回的状态码是 404 - 我只是不明白,因为 Web 服务显然在那里,并且可以使用在线解析器进行解析!
所以仍然卡住了,但至少它指出了问题所在。有人有什么想法可以进一步帮助我吗?
【问题讨论】:
-
我希望所有的问题都和你的一样清楚。但对不起,我不知道答案。您是否收到任何错误消息?您可以告诉服务器的任何响应吗?
-
您是否遇到任何错误?或者你得到一个成功的响应并且响应对象是空的?
-
感谢 cmets。我没有收到任何错误。该代码不会进入任何 NSURLConnectionDelegate 内容。它没有遇到 didFailWithError 或 didCancelAuthenticationChallenge。它只是到达 didReceiveAuthenticationChallenge 并运行: let credentials = NSURLCredential(forTrust: challenge!.protectionSpace.serverTrust) challenge!.sender.useCredential(credentials, forAuthenticationChallenge: challenge!) 然后停止进一步处理!然而,去掉 SSL 的东西,一切正常。
-
我编辑了我的原始帖子,因为 swift 认为它从 https 网络服务返回 404,我只是不明白它在浏览器中工作并使用在线 WDSL 解析器。跨度>
-
已排序!它是 Web 服务本身的 Web 配置。不得不在 Web 配置中从中删除命名绑定,然后帖子突然起作用了,所以它实际上是 SVC Web 服务而不是代码!谢谢你的一切!
标签: ios swift ssl nsurlconnection nsurlconnectiondelegate