我试图在swift中发送http请求。
我得到了一个方法(sendHttpRequest
),它将dictionary<String,AnyObject>
作为参数,并尝试使用php中的一个小服务器向localhost发送请求。当我点击调用此功能的按钮时会发生错误
&#34;可选(错误域= NSURLErrorDomain代码= -1002&#34;不支持的URL&#34; UserInfo = {NSErrorFailingURLStringKey = 46.101.145.122 / salvobertoncini.com / servo.php,NSErrorFailingURLKey = 46.101.145.122 / salvobertoncini .com / servo.php,NSLocalizedDescription =不支持的URL,NSUnderlyingError = 0x1763cef0 {错误域= kCFErrorDomainCFNetwork代码= -1002&#34;(null)&#34;}})&#34;
func sendHttpRequests(data : Dictionary<String, AnyObject>)
{
let session = NSURLSession.sharedSession()
do
{
let request = try NSJSONSerialization.dataWithJSONObject(data, options: .PrettyPrinted)
let url = NSURL ( string : "127.0.0.1/Server/servo.php")
let finalRequest = NSMutableURLRequest(URL: url!)
finalRequest.HTTPMethod = "POST"
finalRequest.HTTPBody = request
let task = session.dataTaskWithRequest(finalRequest){ data,response,error in
if error != nil
{
print("Error -> \(error)")
}
do
{
let result = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String:AnyObject]
print("Result -> \(result)")
}
catch
{
print("Error -> \(error)")
}
}
task.resume()
}
catch
{
print(error)
}
}