Quick-Cocos2dx-Community
Quick-Cocos2dx-Community copied to clipboard
iOS 使用 quick 的 httpRequest 下载图片出现不可达。
使用 HTTPRequest 在 iOS,Mac, Windows 版本下会出现不可达,(需挂梯子)。 测试地址是:
-- 原始地址
local picUrl = "http://graph.facebook.com/694528257411491/picture?width=200&height=200&t=20180421"
-- 重定向后的地址
local picUrl = "https://lookaside.facebook.com/platform/profilepic/?asid=694528257411491&height=200&width=200&ext=1524558242&hash=AeSflWB8JKNSiLAF"
这个地址比较特殊,请求之后会重定向到下面那个地址。
浏览器直接打开这个地址是可以下载得到对应的图片的。
httpRequest 的 test code
function M:onResponse(event, index, dumpResponse)
local request = event.request
printf("REQUEST %d - event.name = %s", index, event.name)
if event.name == "completed" then
printf("REQUEST %d - getResponseStatusCode() = %d", index, request:getResponseStatusCode())
if request:getResponseStatusCode() ~= 200 then
else
printf("REQUEST %d - getResponseDataLength() = %d", index, request:getResponseDataLength())
print("dump:" .. tostring(dumpResponse))
if dumpResponse then
printf("REQUEST %d - getResponseString() =\n%s", index, request:getResponseString())
end
end
elseif event.name ~= "progress" then
-- printf("REQUEST %d - getErrorCode() = %d, getErrorMessage() = %s", index, request:getErrorCode(), request:getErrorMessage())
print("ErrorCode:" .. tostring(request:getErrorCode()))
print("ErrowMsg:" .. tostring(request:getErrorMessage()))
end
print("----------------------------------------")
end
function M:createHTTPRequestTest()
local url = "http://graph.facebook.com/694528257411491/picture?width=200&height=200&t=20180421"
if (not self.requestCount) then
self.requestCount = 0
end
self.requestCount = self.requestCount + 1
local index = self.requestCount
local request = network.createHTTPRequest(function(event)
if tolua.isnull(self) then
printf("REQUEST %d COMPLETED, BUT SCENE HAS QUIT", index)
return
end
self:onResponse(event, index)
end, url, "GET")
printf("REQUEST START %d", index)
request:start()
end
关于 HTTPRequest 底层是采用 curl 实现的,所以猜想是不是因为这个模块的一些请求设置有问题导致的。
在 V3.6.5 版本下本来还有一个 XMLHTTPRequest,就用这个写了个测试类,发现也是有问题的。而在 cocos2dx 3.16 版本上却是可以的。关于 httpClient 的代码:
if (not self.m_testSprite) then
self.m_testSprite = display.newSprite("input your custom pic fileName")
:addTo(self)
:pos(display.cx, display.cy)
end
self.m_testSprite:retain()
local httpRequest = cc.XMLHttpRequest:new()
httpRequest._urlFileName = "test_download.jpg"
httpRequest._urlSprite = self.m_testSprite
httpRequest.responseType = cc.XMLHTTPREQUEST_RESPONSE_STRING
httpRequest:open("GET", "http://graph.facebook.com/694528257411491/picture?width=200&height=200&t=20180421")
local function onDownloadImageCallback( ... )
print(" onDownloadImageCallback -- readyState is: ", httpRequest.readyState, " status is ", httpRequest.status)
if httpRequest.readyState == 4 and (httpRequest.status >= 200 and httpRequest.status < 207) then
local fileData = httpRequest.response
local fullFileName = device.writablePath .. "/" .. httpRequest._urlFileName
local file = io.open(fullFileName, "wb")
file:write(fileData)
file:close()
local texture2d = cc.Director:getInstance():getTextureCache():addImage(fullFileName)
local pSprite = httpRequest._urlSprite
if texture2d then
pSprite:setTexture(texture2d)
end
pSprite:release()
end
end
httpRequest:registerScriptHandler(onDownloadImageCallback)
httpRequest:send()
有没有老司机测试看看。
目前在路由器翻墙的情况下可以下载得到图片了,但是通过手机安装的 ss 的话还是无法下载。
这个可能是代理连接的问题。需要看看