Is there any well commented example of sending http get request?
Or any library that allows you to skip this low level initialization routine (which I can't understand) and is also well documented?
Btw,sorry for mah engrish
Advertising
Code: Select all
int oslNetGetFile(const char *url, const char *filepath)
{
int template, connection, request, ret, status, dataend, fd, byteswritten;
//SceULong64 contentsize;
unsigned char readbuffer[8192];
/* //<-- STAS: HTTP library was already initialized (see oslNetInit()) !
ret = sceHttpInit(20000);
if(ret < 0)
return OSL_ERR_HTTP_INIT;
*/ //<-- STAS END -->
template = sceHttpCreateTemplate("OSL-agent/0.0.1 libhttp/1.0.0", 1, 1);
if(template < 0)
return OSL_ERR_HTTP_TEMPLATE;
ret = sceHttpSetResolveTimeOut(template, 3000000);
if(ret < 0)
return OSL_ERR_HTTP_TIMEOUT;
ret = sceHttpSetRecvTimeOut(template, 60000000);
if(ret < 0)
return OSL_ERR_HTTP_TIMEOUT;
ret = sceHttpSetSendTimeOut(template, 60000000);
if(ret < 0)
return OSL_ERR_HTTP_TIMEOUT;
connection = sceHttpCreateConnectionWithURL(template, url, 0);
if(connection < 0)
return OSL_ERR_HTTP_CONNECT;
request = sceHttpCreateRequestWithURL(connection, PSP_HTTP_METHOD_GET, (char*)url, 0);
if(request < 0)
return OSL_ERR_HTTP_REQUEST;
ret = sceHttpSendRequest(request, 0, 0);
if(ret < 0)
return OSL_ERR_HTTP_REQUEST;
ret = sceHttpGetStatusCode(request, &status);
if(ret < 0)
return OSL_ERR_HTTP_GENERIC;
if(status != 200)
return 0;
/* Strangelove fix
ret = sceHttpGetContentLength(request, &contentsize);
if(ret < 0)
return OSL_ERR_HTTP_GENERIC;
*/
dataend = 0;
byteswritten = 0;
// Strangelove fix - Added PSP_O_TRUNC
fd = sceIoOpen(filepath, PSP_O_WRONLY | PSP_O_TRUNC | PSP_O_CREAT, 0777);
while(dataend == 0)
{
ret = sceHttpReadData(request, readbuffer, 8192);
if(ret < 0)
{
sceIoWrite(fd, filepath, 4);
sceIoClose(fd);
return OSL_ERR_HTTP_GENERIC;
}
if(ret == 0)
dataend = 1;
if(ret > 0)
{
byteswritten += ret;
sceIoWrite(fd, readbuffer, ret);
}
}
sceIoClose(fd);
sceHttpDeleteRequest(request);
sceHttpDeleteConnection(connection);
sceHttpDeleteTemplate(template);
// sceHttpEnd(); //<-- STAS: This should be done in oslNetTerm() only !
return 0;
}