我尝试使用来自iOS(swift语言)的某些网站的发布参数(用户名,密码)的URLRequest,操作成功,我从网上收到消息成功,告诉我你已登录
在它下面是我的代码:
// check the user name and password
guard let url = URL(string: "http://MyWeb/login.php")else {
displayAlertControllerForLogin(title: "Wrong URL", message: "Please Check URL")
return
}
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "POST"
let postString = "username=\(userName)&password=\(password)"
urlRequest.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: urlRequest) {[weak self] (data, response, error) in
if error != nil {
print("have an error")
return
}// end the if error
let string = String(data: data!, encoding: .utf8)
if let string = string {
if string.contains("cel_song") {
DispatchQueue.main.async { [weak self] in
// present main view for check only
let tabBarController = MainVC()
self?.present(tabBarController, animated: true, completion: nil)
}// end the dispatch
}else{
// show the alert if the user invalid
DispatchQueue.main.async { [weak self] in
self?.displayAlertControllerForLogin(title: "Error Login!", message: "Please Check User Name / Password")
}// end the dispatch
}// end the else
}// end the if let string
}// end the task
task.resume()
现在,我需要使用URLrequest(用户名,全名,电子邮件,网站,密码,confirmPassword)通过post 5参数注册到本网站,以便在该网站注册 但操作失败了 以下是我使用urlRequest帖子在网站注册的代码:
guard let url = URL(string: "http://MyWeb/register.php") else {displayAlertControllerForLogin(title: "Wrong URL", message: "Please ReWrite URL") ; return }
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "POST"
let webSite = "google"
let postString = "username=\(userName)&fullname=\(fullName)&email=\(email)&website=\(webSite)&password=\(password)&confirmPassword=\(confirmPassword)"
urlRequest.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: urlRequest, completionHandler: {[weak self] (data, response, error) in
if let error = error {
self?.displayAlertControllerForLogin(title: "Error Issue", message: error.localizedDescription)
return
}
// fetch the data
let string = String(data: data!, encoding: .utf8)
if let string = string {
if string.contains("Registration...") {
// register faield
DispatchQueue.main.async { [weak self] in
self?.displayAlertControllerForLogin(title: "Register Faield!", message: "Some Issue Occur When Try To register!")
print(string)
return
}
}else{
// register complete
DispatchQueue.main.async { [weak self] in
self?.displayAlertControllerForLogin(title: "Register Complete!", message: "Welcome \(userName) You Are Register To Ampache Successfully!")
return
}
}// end the if string.contains
}// end the if let
})// end the task
task.resume()
我的问题是否可以使用快速语言中的帖子5参数使用网址请求注册到某个网站?