ios-swift3.0请求https封装工具类(get请求和post请求).zip

-
swift3.0请求https封装工具类(get请求和post请求)
-
2019-07-11
33KB
ios-原滋原味的 swift post 网络请求.zip
2019-07-11闲着没事的时候写的swift网络请求,没封装,可以随便用用
使用Swift发布POST请求_course
2015-01-03<div class="post-text" itemprop="text"> <p>I'm trying to send data over from my iOS app to a PHP script. I have Title, Description and City. I want it to always save every info I send to it from my iOS app and always make a new one every time I send it data. The problem I'm having is, when I go to the PHP file on my browser, nothing shows up. The title, description and city field is always blank. But in my Xcode console I see it returning everything correctly. Here's my swift code. </p> <pre><code>func postToServerFunction() { var url: NSURL = NSURL(string: "http://vittletest.netii.net/index.php")! var request:NSMutableURLRequest = NSMutableURLRequest(URL:url) request.HTTPMethod = "POST" let postString = "title=testing&description=hello&city=RanchoCordova" request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding) let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in if error != nil { println("error=\(error)") return } println("response = \(response)") let responseString = NSString(data: data, encoding: NSUTF8StringEncoding) println("responseString = \(responseString)") } task.resume() } </code></pre> <p>And here's my PHP file. </p> <pre><code><?php $something = "Some Info"; echo $something; $title = $_POST['title']; $description = $_POST['description']; $city = $_POST['city']; echo "<br />"; echo "Title: ". $title; echo "<br />"; echo "Description: ". $description; echo "<br />"; echo "City: ". $city; ?> </code></pre> <p>And here's my console output on Xcode. </p> <pre><code>2015-01-03 10:05:50.861 VittleTest[616:28516] Warning: A long-running operation is being executed on the main thread. Break on warnBlockingOperationOnMainThread() to debug. response = <NSHTTPURLResponse: 0x7f9f5ae5e3f0> { URL: http://vittletest.netii.net/index.php } { status code: 200, headers { Connection = close; "Content-Length" = 309; "Content-Type" = "text/html"; Date = "Sat, 03 Jan 2015 18:05:52 GMT"; Server = Apache; "X-Powered-By" = "PHP/5.2.17"; } } responseString = Optional(<html> <head> <title>PHP Test</title> </head> <body> Some Info<br />Title: testing<br />Description: hello<br />City: RanchoCordova </body> </html> <!-- Hosting24 Analytics Code --> <script type="text/javascript" src="http://stats.hosting24.com/count.php"></script> <!-- End Of Analytics Code --> ) </code></pre> <p>I hope I gave you guys enough info to help me out because I can't seem to figure it out since I'm not the best with PHP.</p> </div>
在Swift 4中发出HTTP POST请求_course
2018-01-09<div class="post-text" itemprop="text"> <p>I´m trying to send data to a PHP file on my server using <code>URLSession</code>. This seems to fail though, since no data is received on the server. While running the app, I get this console print:</p> <pre><code>2018-01-09 20:56:10.942359+0100 EinsatzPush[12182:8624757] [BoringSSL] Function boringssl_session_errorlog: line 2871 [boringssl_session_read] SSL_ERROR_ZERO_RETURN(6): operation failed because the connection was cleanly shut down with a close_notify alert 2018-01-09 20:56:10.942857+0100 EinsatzPush[12182:8624757] [BoringSSL] Function boringssl_session_errorlog: line 2871 [boringssl_session_read] SSL_ERROR_ZERO_RETURN(6): operation failed because the connection was cleanly shut down with a close_notify alert 2018-01-09 20:56:10.943028+0100 EinsatzPush[12182:8624757] TIC Read Status [1:0x1c0173680]: 1:57 2018-01-09 20:56:10.943187+0100 EinsatzPush[12182:8624757] TIC Read Status [1:0x1c0173680]: 1:57 2018-01-09 20:56:10.943278+0100 EinsatzPush[12182:8624757] TIC Read Status [1:0x1c0173680]: 1:57 2018-01-09 20:56:10.943923+0100 EinsatzPush[12182:8624757] [BoringSSL] Function boringssl_session_errorlog: line 2871 [boringssl_session_read] SSL_ERROR_ZERO_RETURN(6): operation failed because the connection was cleanly shut down with a close_notify alert </code></pre> <p>This is the code I´m using:</p> <pre><code>let url = URL(string: "https://my-server.com/update.php") let session = URLSession.shared let request = NSMutableURLRequest(url: url as! URL) request.httpMethod = "POST" let paramString = "username=user&token=token" request.httpBody = paramString.data(using: String.Encoding.utf8) let task = session.dataTask(with: request as URLRequest) { ( data, response, error) in guard let _:NSData = data as NSData?, let _:URLResponse = response, error == nil else { print("error") return } if let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) { print(dataString) } } task.resume() </code></pre> <p>I´d be very happy if anyone here could help me get this fixed. Thanks!</p> </div>
-
博客
java方法详解
java方法详解
-
学院
【数据分析-随到随学】Hadoop数据分析
【数据分析-随到随学】Hadoop数据分析
-
博客
WPF数据查询
WPF数据查询
-
下载
腾讯研发人员能力素质模型.zip
腾讯研发人员能力素质模型.zip
-
学院
三维地图GIS大数据可视化
三维地图GIS大数据可视化
-
学院
SubstancePainter插件开发-基础入门
SubstancePainter插件开发-基础入门
-
学院
Java Web开发之Java语言基础
Java Web开发之Java语言基础
-
下载
DragPictureDisplay.rar
DragPictureDisplay.rar
-
下载
武汉大学《线性代数》历年真题及答案.pdf
武汉大学《线性代数》历年真题及答案.pdf
-
下载
四路智能抢答器Proteus schematic.zip
四路智能抢答器Proteus schematic.zip
-
下载
windows下的nc命令工具.zip
windows下的nc命令工具.zip
-
下载
大学《物联网》必背知识点.pdf
大学《物联网》必背知识点.pdf
-
下载
DotNetBar for Windows Forms 12.7.0.10.rar
DotNetBar for Windows Forms 12.7.0.10.rar
-
博客
Sentinel(五)之流量控制
Sentinel(五)之流量控制
-
下载
9277复现的yolov3代码.zip
9277复现的yolov3代码.zip
-
博客
CV小白论文阅读之-ACFNet: Attentional Class Feature Network for Semantic Segmentation
CV小白论文阅读之-ACFNet: Attentional Class Feature Network for Semantic Segmentation
-
学院
量化高频交易系统tick数据采集
量化高频交易系统tick数据采集
-
下载
StudentManagt.zip
StudentManagt.zip
-
博客
Python2与Python3的编码问题(Linux和Windows编码转换)
Python2与Python3的编码问题(Linux和Windows编码转换)
-
学院
微服务系列第七十一季-Spring入门
微服务系列第七十一季-Spring入门
-
下载
海南大学《数据库》知识点总结.pdf
海南大学《数据库》知识点总结.pdf
-
下载
小白云净重装装机大师
小白云净重装装机大师
-
下载
北京理工大学《计算机系统结构》期末试卷.pdf
北京理工大学《计算机系统结构》期末试卷.pdf
-
博客
利用NB-IoT技术实现数据上传至阿里云
利用NB-IoT技术实现数据上传至阿里云
-
博客
TensorFlow2.0环境的配置
TensorFlow2.0环境的配置
-
学院
30个生涯锦囊,带你跳出迷茫,找到适合你的职业方向
30个生涯锦囊,带你跳出迷茫,找到适合你的职业方向
-
学院
转行做IT-第2章 HTML入门及高级应用
转行做IT-第2章 HTML入门及高级应用
-
博客
大量数据推送kafka,进行数据上报
大量数据推送kafka,进行数据上报
-
博客
数据表格的数据查询
数据表格的数据查询
-
学院
计算机网络基础
计算机网络基础