我需要修改NSURLResponse中的響應標頭。這可能嗎?Mutable NSHTTPURLResponse或NSURLResponse
回答
您可以使用allHeaderFields
方法將它們讀入NSDictionary。
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSDictionary *httpResponseHeaderFields = [httpResponse
allHeaderFields];
是100%安全的,你不會想和
if ([response respondsToSelector:@selector(allHeaderFields)]) {... }
這不是返回一個NSDictionary而不是一個NSMutableDictionary? – 2010-01-19 20:15:20
是的,這就是代碼示例中的內容。以下是課程參考資料http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSHTTPURLResponse_Class/Reference/Reference.html#//apple_ref/occ/instm/NSHTTPURLResponse/allHeaderFields – shawnwall 2010-01-19 20:30:03
I don看不到不可改變的字典將如何幫助我修改鍵/值 – 2010-01-19 20:34:46
我只是說這與一個朋友把它包起來。我的建議是寫一個NSURLResponse的子類。沿着這些路線的東西:
@interface MyHTTPURLResponse : NSURLResponse { NSDictionary *myDict; }
- (void)setAllHeaderFields:(NSDictionary *)dictionary;
@end
@implementation MyHTTPURLResponse
- (NSDictionary *)allHeaderFields { return myDict ?: [super allHeaderFields]; }
- (void)setAllHeaderFields:(NSDictionary *)dict { if (myDict != dict) { [myDict release]; myDict = [dict retain]; } }
@end
如果你正在處理一個對象,你沒有做,你可以嘗試使用object_setClass
到調酒類的。但是我不知道這是否會添加必要的實例變量。你也可以使用objc_setAssociatedObject
,如果你能支持一個足夠新的SDK,那麼你可以把它全部放在一個類別中。
我有一個類似的問題。我想修改http url響應的頭文件。我需要它,因爲我想爲UIWebView提供緩存的url響應,並且想欺騙Web視圖,即響應未過期(即,我想更改標題的「Cache-Control」屬性,但保留標題的其餘部分)。我的解決方案是使用NSKeyedArchiver對原始http響應進行編碼,並使用委託攔截序列化。在
-(id) archiver:(NSKeyedArchiver*) archiver willEncodeObject:(id) object
我檢查,如果對象是NSDictionary中,如果是,我回來改性字典(即更新爲「緩存控制」報頭)。之後我使用NSKeyedUnarchiver對序列化的響應進行反序列化。當然,您可以掛鉤到解析器並修改其委託中的標題。
注意,在iOS 5中蘋果公司已經加入
-(id)initWithURL:(NSURL*) url statusCode:(NSInteger) statusCode HTTPVersion:(NSString*) HTTPVersion headerFields:(NSDictionary*) headerFields
這是不是在文檔(文檔錯誤),但它是NSHTTPURLResponse
的公共API中你能做到這一點,你'd需要NSHTTPURLResponse
而不是NSURLResponse
,因爲在Swift中,NSURLResponse
可以與許多協議一起使用,而不僅僅用於http
,如ftp
,data:
或https
。因此,您可以調用它來獲取元數據信息,例如預期的內容類型,MIME類型和文本編碼,而NSHTTURLResponse
是負責處理HTTP協議響應的人員。因此,它是操縱標題的人。
這是一個小代碼,它處理響應中的標題密鑰Server
,並在更改前後輸出值。
let url = "https://www.google.com"
let request = NSMutableURLRequest(URL: NSURL(string: url)!)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
if let response = response {
let nsHTTPURLResponse = response as! NSHTTPURLResponse
var headers = nsHTTPURLResponse.allHeaderFields
print ("The value of the Server header before is: \(headers["Server"]!)")
headers["Server"] = "whatever goes here"
print ("The value of the Server header after is: \(headers["Server"]!)")
}
})
task.resume()
- 1. NSHTTPURLResponse變爲NSURLResponse
- 2. 什麼時候NSURLResponse不是NSHTTPURLResponse?
- 3. 從'NSURLResponse *'分配給'NSHTTPURLResponse *'的指針類型不相容
- 4. 夫特 - 低垂NSURLResponse到NSHTTPURLResponse爲了得到響應代碼
- 5. NSHTTPURLResponse
- 6. 讀NSURLresponse
- 7. NSURLResponse返回nil?
- 8. NSURLResponse expectedContentLength -1
- 9. Mutable vs. Immutable
- 10. Mapbox iOS Mutable RMPolylineAnnotation
- 11. Scala Mutable Option?
- 12. Python mutable NamedTuple
- 13. 如何獲取NSURLResponse
- 14. NSHTTPURLResponse釋放消息
- 15. 什麼是語義含義:volatile-mutable與:unynchronized-mutable?
- 16. 爲什麼是KindOfClass:[NSHTTPURLResponse self]而不是isKindOfClass:[NSHTTPURLResponse class]?
- 17. 如何獲得NSURLResponse主體?
- 18. 從NSURLResponse提取數據
- 19. impl convert ::從for(mutable)reference
- 20. Elementary Mutable Haskell哈希表
- 21. python:「mutable vectors are unshashable」error
- 22. 返回R中Mutable或Immutable變量的函數
- 23. 檢測HTML編碼時NSURLResponse爲textEncodingName
- 24. 如何訪問ViewController中的NSURLResponse
- 25. 我可以通過NSURLResponse獲取URL嗎?
- 26. 從NSURLResponse中檢測NSStringEncoding的函數?
- 27. 如何訪問不同類中的NSURLResponse
- 28. NSURLResponse的expectedContentLength的替代方案
- 29. 出NSURLResponse完成塊的獲取數據
- 30. 爲什麼NSHTTPURLResponse返回nil? NSURLSession,Swift 2.2
您是否嘗試修改從服務器獲取的標頭?你的意思是NSURLRequest? – notnoop 2010-01-19 20:09:44
wait ... no ..我的意思是NSURLResponse – 2010-01-19 20:26:38