2017-07-22 6 views
-1

私は、HTTPリクエストをSwiftで実行しようとしています。POST 2パラメータをURLに適用しようとしています。 しかし、それは常に403エラーページを返します、何が問題なのですか?URLSessionはHTTP 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() 
+0

postStringを辞書ではなく文字列として使用します。またtryを使用しますか? JSONSerialization.data(withJSONObject:postString、options:.prettyPrinted) –

+0

私はちょうどそれを更新し、まだ動作しません:( –

+0

サーバは独自の方法でPOSTデータを受け取ります。 – OOPer

答えて

0

エンドポイントはhttp://www.portaljuridico.com.co/portaljuridico/User/loginUserように表示され、無効なユーザー/パスワードにhttp://www.portaljuridico.com.co/portaljuridico/Loginにリダイレクトします。

関連する問題