【问题标题】:Is there any same method in swift to read InpuStream using post http requestswift中有没有相同的方法使用post http请求读取InpuStream
【发布时间】:2020-02-14 13:42:42
【问题描述】:

在 swift 中是否存在任何相同的方法来从 HTTP 请求中读取输入流

InputStream in = address.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder result = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
    result.append(line);
}
System.out.println(result.toString());

这是一个本地服务器正在发送两个带有 207 的响应:

var http = require('http');
var express = require('express')();

var port = process.env.PORT || 3000;
var promise = new Promise(function(resolve, reject) { 
  const x = "geeksforgeeks"; 
  const y = "geeksforgeeks"
  if(x === y) { 
    console.log('resolve');
    resolve(); 
  } else { 
    reject(); 
  } 
}); 

express.post('/', function(req, res) {
  console.log('send req1')
  // check if network exists and user requesting is owner of it
  return promise.then(() => {
    // add listener to receive response from gateway and forward it
    //_addGwEmitter.addGwEmitter.addEventListener(req, res, gatewayPsn);
    // send the gateway trigger instructions to coco user
    res.status(207).write(JSON.stringify({
      status: 200,
      msg: "Waiting for authorization\n",
      instructionText: "devProductInfo.instructionText",
      instructionImage: "devProductInfo.instructionImageURL"
    }) + "\n \r End" );
    // if no event is received from gateway trigger timeout after 60 seconds
                      res.setTimeout(6000,()=>{
    console.log('send req 2');
                                     res.status(207).write(JSON.stringify({
                                       status: 200,
                                       msg: "authorization done \n",
                                       instructionText: "devProductInfo.instructionText",
                                       instructionImage: "devProductInfo.instructionImageURL"
                                     }));
                                     res.end();
                                     });
  }).catch(error => {
    return res.status(400).send("error.getErrorInfo()");
  });


});

http.createServer(express).listen(port);

我想一一阅读两个响应 我努力了 上传任务 下载任务 数据任务

在 HTTP URLSession 中。

【问题讨论】:

    标签: ios swift http input


    【解决方案1】:

    如果您想在 iOS 设备中使用 207 响应然后使用委托和数据委托实现 URL 数据任务,我得到了答案,您将获得响应数据。确保响应内容类型是 text/json

    【讨论】:

      猜你喜欢
      • 2014-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-02
      相关资源
      最近更新 更多