网络API可在iPhone中返回HTTPS响应 [英] Network API to return HTTPS response in iPhone

查看:103
本文介绍了网络API可在iPhone中返回HTTPS响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用iPhone中的AFNetworking,MKNetworkKit等任何网络API将服务器响应从任何函数返回到其调用函数.

当前,我正在使用NSURLRequest的httpGet函数.我做了以下函数,但无法将服务器响应返回给它调用函数.请帮助我.

Currently I am using httpGet function of NSURLRequest. I made the following function but I am not able to return the server response to it calling function. Please help me.

func connserv(jsonString:NSDictionary) -> NSDictionary{
    var abc: NSDictionary?
    // This is the action performed when clicked on the Connect button on the connectivity screen
    println("------------------Function connserv")

    let prefs = NSUserDefaults.standardUserDefaults()
    var IP: AnyObject = prefs.objectForKey("IP")!
    var port: AnyObject = prefs.objectForKey("Port")!
    println("IP in Connection : \(IP)")
    println("port in Connection : \(port)")
    prefs.synchronize()
    //var learn = LearnNSURLSession()
    let localizedModel = UIDevice.currentDevice().localizedModel
    let model = UIDevice.currentDevice().model
    let devicesystemVersion = UIDevice.currentDevice().systemVersion

    println("HTTP request jsonString : \(jsonString)")
    var request = NSMutableURLRequest(URL: NSURL(string: "https://\(IP):\(port)/")!)

    var response: NSURLResponse?
    var error: NSError?

    //println("HTTP request jsonString : \(jsonString)")

    var err: NSError?
    request.HTTPBody = NSJSONSerialization.dataWithJSONObject(jsonString, options: nil, error: &err)
    request.HTTPMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.setValue("application/json", forHTTPHeaderField: "Accept")
    // send the request
    var learn = LearnNSURLSession()
    println("HTTP request : \(request)")
    learn.httpGet(request) {
        (resultString, error) -> Void in
        if error != nil
        {
            println("completion block")

        }
        else
        {
            let data = (resultString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
            var er: NSError?
            let JSONdata: AnyObject = NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers,error: &er)!

            let abc: AnyObject = NSJSONSerialization.JSONObjectWithData(data!, options: .MutableLeaves, error:&er)!

            println("abc : \(abc)")
            println("JSONdata : \(JSONdata)")

            learn.callback(result: resultString, error: error)
        }
    }
    //return abc!;
}

推荐答案

我不知道您的LearnNSURLSession类是如何工作的,所以我无法提出解决方案.但是这是如何从NSURLConnection查找返回的标头和状态以及返回的数据和任何错误信息,该文档记录在

I do not know how your LearnNSURLSession class works, so I cannot suggest a solution with that. But here is how to find the returned headers and status, together with the returned data and any error information, from an NSURLConnection, documented here:

    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.currentQueue(), completionHandler: {
        response, data, error in
        if let response = response as? NSHTTPURLResponse {
            debugPrintln(response.allHeaderFields)
            let statusCode = response.statusCode
            debugPrintln("statusCode: \(statusCode): \(NSHTTPURLResponse.localizedStringForStatusCode(statusCode))")
        } else { println("That's odd.") }
    })

尽管该文档表明响应是NSURLResponse,但

While that documentation suggests that the response is an NSURLResponse, this page makes clear that you will get a NSHTTPURLResponse which contains all the headers and status.

如果您需要一个同步版本,它将停止该功能,直到获得结果为止,您可以使用

If you need a synchronous version, that will stop the function until it gets a result, you can use this call instead, like this:

var response:NSURLResponse?
var e: NSError?
NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &e)
debugPrintln(response)

这将使您可以将有意义的结果返回给呼叫者.

That will allow you to return a meaningful result to the caller.

这篇关于网络API可在iPhone中返回HTTPS响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆