2016-03-02 55 views
0

我想創建一個使用NSURLSession的多部分請求,但我無法獲得可解碼的請求工作。我幾乎看到每個帖子都在這裏,而這個代碼是幾個帖子的組合。創建多部分機構請求swift

/// Create custom OAuth2Request Measurement request. 
    /// - Parameter data: Data to be sent. 
    /// - Returns: NSMutableURLRequest 
    internal func createOAuth2MeasurementPOSTRequest(f_fileData: NSData, f_fileName: String) -> NSMutableURLRequest { 

     //Request Data 
     let data = NSMutableData() 

     // Create URL 
     let target_URL = NSURL(string: target_URL_string) 

     let req = OAuthManager.sharedInstance.oauth2.request(forURL:target_URL!) 

     // Set method to POST 
     req.HTTPMethod = "POST" 

     // ignore cacheing 
     req.cachePolicy = .ReloadIgnoringLocalCacheData 

//  let boundary = generateBoundaryString() 
//  let contentType = "multipart/form-data; boundary=\(boundary)" 
//  req.setValue(contentType, forHTTPHeaderField:"Content-Type") 


     // Get id 
     if let tenID = NSUserDefaults.standardUserDefaults().objectForKey(Constants.TENANT_ID) { 
//   // Append disposition data 
//   let tenantLocationDictionary:[String:String] = [ 
//    "TenantId": tenID as! String, 
//    // location ID needs to be dynamic 
//    "LocationId": "7688f946-a558-49db-9477-098e073ad514" 
//   ] 

      let boundry:String = generateBoundaryString() 
      let MPboundary:String = "\(boundry)" 
      let endMPboundary:String = "\(MPboundary)" 
      let data:NSData = f_fileData 
      let body:NSMutableString = NSMutableString(); 

      let content:String = "multipart/form-data; boundary=\(boundry)" 
      req.setValue(content, forHTTPHeaderField: "Content-Type") 

      body.appendFormat("\(MPboundary)\r\n") 
      body.appendFormat("Content-Disposition: form-data; name=\"moldel") 
      body.appendFormat("{\"TenantId\": \"\(tenID)", "LocationId\":\"7688f946-a558-49db-9477-098e073ad514\"}\r\n") 
      let end:String = "\(endMPboundary)" 
      body.appendFormat("Content-Disposition: form-data; name=\"\(f_fileName)\"; filename=\"(f_fileName)\"\"\r\n") 
      body.appendFormat("Content-Type: text/xml\r\n\r\n") 
      let myRequestData:NSMutableData = NSMutableData(); 
      myRequestData.appendData(body.dataUsingEncoding(NSUTF8StringEncoding)!) 
      myRequestData.appendData(data) 
      myRequestData.appendData(end.dataUsingEncoding(NSUTF8StringEncoding)!) 
      req.setValue("\(myRequestData.length)", forHTTPHeaderField: "Content-Length") 
      req.HTTPBody = myRequestData 
      req.HTTPMethod = "POST" 
     } 

     // append filedata 
     data.appendData(f_fileData) 

     // Add Data to body 
     req.HTTPBody = data 

     return req 
    } 

    /// Creates a GET request and returns it. 
    /// - Parameter None: 
    /// - Returns: NSMutableURLRequest 
    internal func createOAuth2GETRequest() -> NSMutableURLRequest { 
     // Create URL 
     let target_URL = NSURL(string: target_URL_string) 

     // Create mutable request 
     let req = OAuthManager.sharedInstance.oauth2.request(forURL: target_URL!) 

     // ignore cacheing 
     req.cachePolicy = .ReloadIgnoringLocalCacheData 

     // Return the request. 
     return req 
    } 

我的邊界應該包含tenantId和LocationId的字典。這是用不同語言寫成的一篇成功的文章。

Content-Type: multipart/form-data; boundary=----WebKitFormBoundary1qyYAqC05he9W5sq 
Accept: */* 
Accept-Encoding: gzip, deflate 
Accept-Language: en-US,en;q=0.8 
Cookie: <cookie> 
------WebKitFormBoundary1qyYAqC05he9W5sq 
Content-Disposition: form-data; name="model" 
{"TenantId": "7688f946-a558-49db-9477-098e073ad514", "LocationId":"7688f946-a558-49db-9477-098e073ad514"} 
------WebKitFormBoundary1qyYAqC05he9W5sq 
Content-Disposition: form-data; name="files"; filename="LOG0.XML" 
Content-Type: text/xml 
// Start of file 
<?xml version="1.0" encoding="UTF-16"?> 
... file continues 

我如何能複製斯威夫特這個電話?

+0

[此文章是否有幫助?](http://stackoverflow.com/q/15410689/419348) – AechoLiu

+0

我無法使用AFNetworking。 – Siriss

回答

0

原始請求看起來不對我。主標題AFAIK後應該有一個空行。

即便如此,我看到的主要問題是,每次將邊界插入文件時都會丟失兩個連字符。例如,如果將Content-Type中的邊界設置爲「foo」,那麼只要實際使用邊界,就可以使用「--foo」。