Advertising (This ad goes away for registered users. You can Login or Register)

How to send http get request?

Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
Post Reply
abbateur
Posts: 2
Joined: Sun Oct 18, 2015 1:01 am

How to send http get request?

Post by abbateur » Sun Oct 18, 2015 1:17 am

Ok, I have pspsdk, I looked at examples (at least default htmlviewer example), and I didn't quite understand the code .
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 :roll:
Advertising

User avatar
Joel16
Posts: 912
Joined: Wed Oct 12, 2011 8:47 pm

Re: How to send http get request?

Post by Joel16 » Sun Oct 18, 2015 6:14 am

I'm not sure if this is what you mean, but OSlib had a function to get a file from HTTP:

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;
}
If I recall correctly, I tried this ages ago and it never worked. Honestly that's as easy as it gets, I don't think you can make it any simpler to be honest. :? Maybe some more detailed comments would help. If you had a strong knowledge in C/C++ , it shouldn't be much of a problem though.
Advertising
"Forever in darkness, a guardian devil."

Post Reply

Return to “Programming and Security”