Why is the HTTPBody of a request inside an NSURLProtocol Subclass always nil?

I have my NSURLProtocol MyGreatProtocol . I add it to the URL Loading system,

NSURLProtocol.registerClass(MyGreatProtocol)

I then start receiving events inside MyGreatProtocol during network sessions.

Suppose I create the session after registering my protocol,

let session = NSURLSession.sharedSession()
    let request = NSMutableURLRequest(URL: NSURL(string:"http://example.com")!, cachePolicy: .ReloadIgnoringLocalCacheData, timeoutInterval: 2.0) //arbitrary 404
    request.HTTPBody = ([1, 3, 3, 7] as [UInt8]).toNSData
    print("body data: (request.HTTPBody)") //prints `Optional(<01030307>)`
    request.HTTPMethod = "POST"
    session.dataTaskWithRequest(request) { (data, response, err) in
        print("data: (data)nresponse: (response)nerror(err)")
    }.resume()

I expect the request's HTTP body 1/3/3/7 to be present inside MyGreatProtocol , where it is not.

Inside MyGreatProtocol , I override the following methods.

override class func canonicalRequestForRequest(request: NSURLRequest) -> NSURLRequest {
    print(request.HTTPBody) //prints nil
    return request
}

override class func canInitWithRequest(request: NSURLRequest) -> Bool {
    print(request.HTTPBody) //prints nil
    return true
}

override func startLoading() {
    print(self.request.HTTPBody) //prints nil
}

The other properties of the NSURLRequest seem to be retained. The URL, HTTP verb, headers, etc, are all there. Something specific about the nature of the body remains elusive.

Why is the HTTP Body Nil inside a custom NSURLProtocol?

There seems to be some similar discussion on radars previously filed (ie https://bugs.webkit.org/show_bug.cgi?id=137299)


IIRC, body data objects get transparently converted into streaming-style bodies by the URL loading system before they reach you. If you need to read the data:

  • Open the HTTPBodyStream object
  • Read the body data from it
  • There is one caveat: the stream may not be rewindable, so don't pass that request object on to any other code that would need to access the body afterwards. Unfortunately, there is no mechanism for requesting a new body stream, either (see the README file from the CustomHTTPProtocol sample code project on Apple's website for other limitations).

    链接地址: http://www.djcxy.com/p/34808.html

    上一篇: NSURLProtocol子类会自动更改URL

    下一篇: 为什么NSURLProtocol子类中的请求的HTTPBody始终为零?