我有一种情况需要从回调块中访问AFJSONRequestOperation的原始响应数据,该回调块仅包含NSHTTPURLResponse.我能够从NSHTTPURLResponse获取statusCode,但是没有看到任何获取原始数据的方法.有没有人知道从这个操作的故障回调块访问它的好方法?
这是我天真的第一次密码:
var httpUrlResponse: NSHTTPURLResponse? // = (...get from server...)
let contentType = httpUrlResponse?.allHeaderFields["Content-Type"]
Run Code Online (Sandbox Code Playgroud)
我已经尝试了这个代码的各种派生,但是我一直得到编译器警告/错误,这些警告/错误与allHeaderFields属性的NSDictionary类型之间的基本阻抗不匹配以及我想要获得String或可选String的愿望有关.
只是不确定如何强制这些类型.
我想将我的响应从NSHTTPURLResponse类型转换为String:
let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) -> Void in
println("Response: \(response)")
var responseText: String = String(data: response, encoding: NSUTF8StringEncoding)
})
Run Code Online (Sandbox Code Playgroud)
下面的行将响应消息输出到控制台.
println("Response: \(response)")
Run Code Online (Sandbox Code Playgroud)
但这一行给我一个错误:Call中的额外参数'encoding'.
var responseText: String = String(data: response, encoding: NSUTF8StringEncoding)
Run Code Online (Sandbox Code Playgroud)
如何成功将此"响应"转换为字符串?
转换为Swift 3我注意到从HTTPURLResponse读取头字段时出现了一个奇怪的错误.
let id = httpResponse.allHeaderFields["eTag"] as? String
Run Code Online (Sandbox Code Playgroud)
不再工作了.
我打印出所有标题字典,我的所有标题键似乎都在Sentence案例中.
根据查尔斯代理,我的所有标题都是小写的.根据后端团队的说法,在他们的代码中,标题是Title-Case.根据文档:标题应该不区分大小写.
所以我不知道该相信哪一个.是否有其他人在Swift 3中发现他们的标题现在被iOS转变为句子案例?如果是这样,我们想要的是什么?
我应该记录Apple的错误,还是应该在HTTPURLResponse上创建一个类别,以允许自己不区分大小写地找到标题值.
我有一个非常奇怪的问题,我正在请求一个URL,我想从中获取cookie,我用这种方式来获取cookie:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
NSDictionary *fields = [HTTPResponse allHeaderFields];
NSString *cookie = [fields valueForKey:"Set-Cookie"];
}
Run Code Online (Sandbox Code Playgroud)
但是饼干不完整,有一些字段缺失,我在PostMan上检查过,所有的饼干都在那里.
我在启动时也使用了这种方法NSURLRequest.
[request setHTTPShouldHandleCookies:YES];
Run Code Online (Sandbox Code Playgroud)
问题出在哪儿?
注意:这个问题是在iOS上,我是一个Android版本,它工作正常,所有的cookie都在那里.
我用Xcode6 Beta 2写了一个Swift应用程序,它使用NSURLRequest和等的CFNetwork类进行网络连接NSHTTPURLResponse.
该应用程序适用于iOS 8,当我尝试在iOS 7设备或运行iOS 7的模拟器上运行时,我在启动应用程序时遇到以下错误:
dyld: Symbol not found: _OBJC_CLASS_$_NSHTTPURLResponse
Referenced from: /Users/patrick/Library/Developer/CoreSimulator/Devices/B0A61F43-A67C-4803-8F5D-77C3972107BE/data/Applications/E0C7C89F-9EEE-4893-BE5B-FCC224F2855D/CheckYourWeather.app/CheckYourWeather
Expected in: /Applications/Xcode6-Beta2.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.1.sdk/System/Library/Frameworks/CFNetwork.framework/CFNetwork
in /Users/patrick/Library/Developer/CoreSimulator/Devices/B0A61F43-A67C-4803-8F5D-77C3972107BE/data/Applications/E0C7C89F-9EEE-4893-BE5B-FCC224F2855D/CheckYourWeather.app/CheckYourWeather
Run Code Online (Sandbox Code Playgroud)
我做了一些研究,发现这是一个链接问题.不过,我知道我正在使用的类已经在iOS 7中可用了.
我还尝试CFNetwork.framework在项目设置中添加框架并将其设置为可选,这只会导致App在运行时崩溃.
对我来说令人困惑的部分是:我写了一个测试应用程序,只是将我在主应用程序中使用的代码粘贴到它中,它运行得很好.因此代码可能不是问题.
从模拟器/设备删除应用程序,清理项目并删除Xcode的DerivedData没有解决问题.
更新:
这是导致崩溃的代码:
extension NSURLRequest {
class func plainPostRequest(url: NSURL, httpBody: String) -> NSURLRequest {
let urlRequest = NSMutableURLRequest(URL: url)
urlRequest.HTTPMethod = "POST"
urlRequest.HTTPBody = httpBody.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
urlRequest.setValue("text/plain", forHTTPHeaderField: "Content-Type")
return urlRequest
}
}
Run Code Online (Sandbox Code Playgroud)
但这只是一个例子.任何使用CFNetwork类都会导致应用程序在启动期间崩溃.
我正在使用Apple的NSURLSession#dataTaskWithURL方法执行一个简单的get请求
[[mySession dataTaskWithURL:myUrl completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// fancy code goes here...
}] resume];
Run Code Online (Sandbox Code Playgroud)
如果我想有条件做某事,如果状态代码成功,似乎我必须将NSURLResponse转换为NSHTTPURLResponse .....
NSUInteger statusCode = ((NSHTTPURLResponse *)response).statusCode;
if (!error && statusCode == 200) {
// even fancier code goes here
} else {
// omg!!!!!!!!!
}
Run Code Online (Sandbox Code Playgroud)
...
但是----------我的问题是:为什么!?!?!?!?!?!?!?
为什么Apple会将响应对象传递给一个不知道自己状态的类型?!我必须编写这段代码的事实让我觉得我做的事情非常错误,必须有一个更好的方法来知道它是200,404还是500 ......
我希望我可以将completionHandler的块参数类型更改为NSHTTPURLResponse,但这似乎是不行的!
你好,我需要在从我的webservice加载数据时创建progressView.
实际上expectedContentLength总是返回-1.
看了很多类似的问题后,我的webservice似乎永远不会发送Content-Length:.
然后我用CURL检查,结果如下:
< HTTP/1.1 200 OK
< Date: Thu, 21 Jun 2012 10:04:39 GMT
< Server: Apache/2.2.16 (Debian)
< X-Powered-By: PHP/5.3.3-7+squeeze9
< cache-control: no-cache
< Vary: Accept-Encoding
< Content-Type: text/html; charset=UTF-8
< Content-Length: 3239
< Connection: Keep-Alive
< Set-Cookie: PHPSESSID=u4o4i9dofdgnkfmtnf163635j6; path=/
Run Code Online (Sandbox Code Playgroud)
这是我的代码来捕捉长度
long long expectDataSize;
long long currentDataSize;
....
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response
{
NSLog(@"expect content length %lld", [response expectedContentLength]);
expectDataSize = [response expectedContentLength];
currentDataSize = 0;
}
Run Code Online (Sandbox Code Playgroud)
任何人都已经看到了这个问题?
是否可以在不使用该方法的情况下修改 NSHTTPURLResponse 上的标头
initWithURL:statusCode:HTTPVersion:headerFields:
Run Code Online (Sandbox Code Playgroud)
创建一个新的自定义 NSHTTPURLResponse
nsurlconnection nsurlrequest nsurlprotocol ios nshttpurlresponse
这是一些代码,我们在其中设置了一个NSHTTPURLResponse对象:
NSString * data = @"response successful";
NSUInteger length = [data length];
NSDictionary * headersDict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:length], @"Content-Length", nil];
NSHTTPURLResponse * response = [[NSHTTPURLResponse alloc] initWithURL:[request URL] statusCode:200 HTTPVersion:@"1.1" headerFields:headersDict];
Run Code Online (Sandbox Code Playgroud)
headersDict如果我们将其作为参数,以下代码将在最后一行崩溃。
错误:testReportAppOpenToAdMobWithAppstoreId (AdTrackerTests) 失败:-[__NSCFNumber length]:无法识别的选择器发送到实例 0xf653f40
我不知道为什么会崩溃。调试显示headersDict没问题,我怀疑是苹果的bug。
有任何想法吗?
HTTPURLResponse我正在尝试使用函数访问类( URLResponse)中的 500 个错误的自定义服务器响应正文URLSession.shared.dataTask。我只能访问statusCodeandallHeaderFields但似乎没有帮助。
在java中相当于ex。是HttpURLConnection.getErrorStream(),但我在纯 swift 中找不到类似的东西(我想在不使用第 3 方库的情况下解决这个问题)。
如何获取 500 错误的文本响应?
httpresponse foundation nshttpurlresponse nsurlsession swift
我正在使用NSURLSession从HTTP服务器请求JSON资源.服务器使用Cache-Control来限制资源缓存在客户端上的时间.
这很好用,但是我还想在内存中缓存一个反序列化的JSON对象,因为它经常被访问,同时继续利用NSURLSession中内置的HTTP缓存机制.
我想我可以保存一些HTTP响应头:Content-MD5,Etag以及Last-Modified反序列化的JSON对象(我使用这3个字段,因为我注意到并非所有HTTP服务器都返回Content-MD5,否则它本身就足够了) .下次我收到JSON对象的响应时,如果这3个字段相同,那么我可以重用以前反序列化的JSON对象.
这是一种确定deserizlied JSON仍然有效的可靠方法.如果没有,我如何确定反序列化的对象是否是最新的?
ios ×6
swift ×4
nsurlsession ×3
objective-c ×3
nsurlrequest ×2
xcode6 ×2
afnetworking ×1
cfnetwork ×1
content-type ×1
cookies ×1
crash ×1
foundation ×1
header ×1
http ×1
http-headers ×1
httpresponse ×1
ios5 ×1
ios7 ×1
iphone ×1
nsurlcache ×1
string ×1
swift3 ×1