从Content-Disposition HTTPURLResponse中检索txt文件

时间:2017-01-18 07:07:06

标签: ios objective-c swift

我试图从HTTPURLResponse标头中检索Content-Disposition文件。到目前为止,我只能将Content-Disposition检索为字符串。我想要检索并使用Content-Disposition文件' api.txt'及其内容。

 let stringURL = "https://xxxxxxxxxxxxxxxxx/json?from=from&to=to&text=msg&api_key=key&api_secret=secret"   
 let newString = stringURL.replacingOccurrences(of: " ", with: "%20")             
 let url = NSURL(string: newString)       
 let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: url as! URL)
 let session = URLSession.shared
 let task = session.dataTask(with: urlRequest as URLRequest) {
                    (data, res, error) -> Void in
 let response = res as! HTTPURLResponse!;

 print("response:", res!)
 if let status = response?.statusCode {
       switch(status){
          case 200:         
                 if let contentType = response?.allHeaderFields["Content-Disposition"] {
                       print(contentType)
             }
           default:
                    print("issue")

           }
        }            
    }

task.resume()

来自API I的NSHTTPURLResponse回复如下:

<NSHTTPURLResponse: 0x1708221a0> { URL: https://xxxxxxxxxxxxxxxxx/json?from=from&to=to&text=text&api_key=api&api_secret=secret } { status code: 200, headers {
"Cache-Control" = "max-age=1";
"Content-Disposition" = "attachment; filename=\"api.txt\"";
"Content-Length" = 173;
"Content-Type" = "application/json";
Date = "Wed, 18 Jan 2017 06:00:48 GMT";
Server = nginx;
"Strict-Transport-Security" = "max-age=31536000; includeSubdomains";
"X-Frame-Options" = deny;
"X-XSS-Protection" = "1; mode=block;"; } }

0 个答案:

没有答案