swift-使用NSURLConnection进行数据的提交和获取(POST与GET)

swift-使用NSURLConnection进行数据的提交和获取(POST与GET),第1张

概述   //  Copyright © 2015年 曾志良. All rights reserved. // //使用NSURLConnection进行数据提交获取(POST与GET) import UIKit class LANNetworkNSURLConnection: NSObject,NSURLConnectionDataDelegate {                 //NSU

// copyright © 2015 曾志良. All rights reserved.

//



//使用NSURLConnection进行数据的提交和获取(POSTGET


import UIKit


class LANNetworkNSURLConnection: NSObject,NSURLConnectionDataDelegate {

//NSURLConnection同步post请求

func NSURLConnectionSynchronousRequestPost(urlString:String,body:String){

var url:NSURL!

url=NSURL(string: urlString)

let request=NSMutableURLRequest(URL: url)

//编码数据Post数据

let postData=body.dataUsingEnCoding(NSUTF8StringEnCoding)

request.httpMethod="POST"

request.httpBody=postData

// let body = "score=\(score)&user=\(userID)"//body例子

//响应对象

var response:NSURLResponse?

do{

let received:NSData?=try NSURLConnection.sendSynchronousRequest(request,returningResponse: &response)

let datastring=Nsstring(data: received!,enCoding: NSUTF8StringEnCoding)

print(datastring)

}

catch let error as NSError{

print("错误了\(error)")

}

}

//NSURLConnection异步post请求(弃用)

func NSURLConnectionAsynchronousRequestPost(urlString:String,body:String){

var url:NSURL!

url=NSURL(string: urlString)

let request=NSMutableURLRequest(URL: url)

//编码数据Post数据

let postData=body.dataUsingEnCoding(NSASCIIStringEnCoding)

request.httpMethod="POST"

request.httpBody=postData

var conn:NSURLConnection!

conn=NSURLConnection(request: request,delegate: self)//9,0以后这个方法弃用

conn.start()

print(conn)

}

//NSURLConnection同步GET请求

func NSURLConnectionSynchronousRequestGet(urlString:String){

var url:NSURL!

url=NSURL(string: urlString)

let request=NSMutableURLRequest(URL: url)

//编码数据Post数据

request.httpMethod="GET"

// //NSURLConnection异步GET请求(弃用)

func NSURLConnectionAsynchronousRequestGET(urlString:String){

var url:NSURL!

url=NSURL(string: urlString)

let request=NSMutableURLRequest(URL: url)

//编码数据Post数据

request.httpMethod="GET"

var conn:NSURLConnection!

conn=NSURLConnection(request: request,0)">以后这个方法弃用

conn.start()

print(conn)

}

func connection(connection: NSURLConnection,dIDReceiveResponse response: NSURLResponse)

{

print("请求成功!");

print(response)

}

func connection(connection: NSURLConnection,dIDReceiveData data: NSData)

{

print("请求成功1");

let datastring = Nsstring(data:data,enCoding: NSUTF8StringEnCoding)

print(datastring)

}

func connectionDIDFinishLoading(connection: NSURLConnection)

{

print("请求成功2");

}

}

总结

以上是内存溢出为你收集整理的swift-使用NSURLConnection进行数据的提交和获取(POST与GET)全部内容,希望文章能够帮你解决swift-使用NSURLConnection进行数据的提交和获取(POST与GET)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1082559.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存