URLSession返回HTTP 403错误页面

时间:2017-07-22 01:03:51

标签: swift post httprequest nsurlsession http-status-code-403

我正在尝试在Swift中运行HTTP请求,将POST 2参数发送到URL。 但它总是返回403错误页面,出了什么问题?

我尝试添加Content-type标头,但它不起作用。

代码:

    print("ENVIAR DATOS")

    let url = NSURL(string: "https://www.portaljuridico.com.co/portaljuridico/User/login")

    let request = NSMutableURLRequest(url: url! as URL)
    request.httpMethod = "POST"

    let param = ["username" : "juan", "password" : "123"]
    guard let httpBody = try? JSONSerialization.data(withJSONObject: param, options: []) else { return }

    //request.addValue("application/form-data", forHTTPHeaderField: "Content-Type")
    //request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    //request.addValue("application/json", forHTTPHeaderField: "Accept")
    request.httpBody = httpBody

    let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in
        guard let data = data, error == nil else {
            print("error = \(error!)")
            return
        }

        if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
            print("statusCode should be 200, but is \(httpStatus.statusCode)")
            print("response = \(response!)")
        }

        let responseString = String(data: data, encoding: .utf8)
        print("responseString = \(responseString!)")
    }

    task.resume()

1 个答案:

答案 0 :(得分:0)

端点似乎是http://www.portaljuridico.com.co/portaljuridico/User/loginUser,并在无效的用户/密码上重定向到http://www.portaljuridico.com.co/portaljuridico/Login