从ASP.net中的Swift / iOS客户端请求接收POST数据

时间:2016-07-06 16:45:34

标签: c# ios asp.net swift

我目前正在编写一个Swift应用程序,我想将包含JSON字符串的请求发送到我的.NET后端/ Web服务。但是,在以下代码中使用此代码时:

ASP.NET:

String jsonString = Request.Form["jsonString"];

夫特:

 let url:NSURL = NSURL(string: "https://www.backend.com/backend.aspx")!;
    let request = NSMutableURLRequest(URL: url);
    request.HTTPMethod = "POST";
    request.HTTPBody = ("jsonString=" + jsonString).dataUsingEncoding(NSUTF8StringEncoding)!;

    let session = NSURLSession.sharedSession()
    let task = session.dataTaskWithRequest(request) {
        (
        let data, let response, let error) in

        guard let _:NSData = data, let _:NSURLResponse = response  where error == nil else {
            print("error")
            return
        }

        let dataString = NSString(data: data!, encoding: NSUTF8StringEncoding)
        print(dataString)

    }

    task.resume()    }

我的asp.net后端的字符串结果为null。无论出于何种原因,我似乎无法从请求中获取字符串。有什么想法吗?

提前致谢

1 个答案:

答案 0 :(得分:0)

我使用Alamofire模块来处理POST请求。

        Alamofire.request(.POST, "someWebSite/api/someApi", parameters: ["Name":"Pancakes", "Price":9.99])
        .responseJSON{response in
            print("1", response.request)  // original URL request
            print("2", response.response) // URL response
            print("3", response.data)     // server data
            print("4", response.result)   // result of response serialization

            if let JSON = response.result.value {
                print("JSON: \(JSON)")
            }
    }