Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. //创建请求体
  2. let param = ["moblie":"18392387159"]
  3.  
  4. let data = try! JSONSerialization.data(withJSONObject: param, options: JSONSerialization.WritingOptions.prettyPrinted)
  5. var string = "json="
  6.  
  7. let Str = String(data: data, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
  8. //拼接
  9. string = string + Str!
  10.  
  11. let Url = URL.init(string: "http://huixin.smartdot.com:9901/GoComWebService/restful/GoComeRestful/getResetCode")
  12.  
  13. let request = NSMutableURLRequest.init(url: Url!)
  14.  
  15. request.timeoutInterval = 30
  16. //请求方式,跟OC一样的
  17. request.httpMethod = "POST"
  18. request.httpBody = string.data(using: String.Encoding.utf8)
  19.  
  20. let session = URLSession.shared
  21. let dataTask = session.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
  22. if (error != nil) {
  23. return
  24. }
  25. else {
  26. //此处是具体的解析,具体请移步下面
  27. let json: Any = try! JSONSerialization.jsonObject(with: data!, options: [])
  28. if let value = JSON(json)["status"].string {
  29. print("状态是:(value)")
  30. }
  31. print(json)
  32. }
  33. }
  34.  
  35. dataTask.resume()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement