MozillaZine

Calling a web request using XPCOM in C++ (XMLHttpRequest)

Discuss building things with or for the Mozilla Platform.
Kirank
 
Posts: 7
Joined: March 24th, 2009, 10:02 pm

Post Posted June 3rd, 2009, 10:42 pm

Hello there,

I've been trying to call a web service request using XPCOM in C++ program. The code snippet goes as,

nsCOMPtr<nsIXMLHttpRequest> pXMLHttpRequest;
nsresult rv;
const nsAString& empty = EmptyString();
pXMLHttpRequest = do_CreateInstance(NS_XMLHTTPREQUEST_CONTRACTID, &rv );
if (!NS_FAILED(rv))
{
int i;

rv = pXMLHttpRequest->Open(NS_LITERAL_CSTRING("POST"),NS_LITERAL_CSTRING("http://dl.google.com/googletalk/googletalk-setup-en-GB.exe"));//,false,empty,empty);
pXMLHttpRequest->SetRequestHeader(NS_LITERAL_CSTRING("Content-type"),NS_LITERAL_CSTRING("octet-stream"));
//pXMLHttpRequest->OverrideMimeType(NS_LITERAL_CSTRING("octet-stream"));
pXMLHttpRequest->Send(NULL);
if(NS_FAILED(rv))
{
i=1;
}
else
{
i=0;
}
}

But i never get the value of i as 0. I am not sure what might be causing this problem as I am new to XPCOM. Please help.
Thanks in anticipation.

jcrosson
 
Posts: 7
Joined: April 20th, 2009, 2:45 pm

Post Posted June 5th, 2009, 7:22 am

I just took a brief look at the code so there could be things I missed...
The C++ XMLHttpRequest requires an "init" providing a "principal".
Here's a bit of my code:

nsCOMPtr<nsIScriptSecurityManager> secMan = do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID);
if (!secMan)
...
nsCOMPtr<nsIPrincipal> systemPrincipal;
rv = secMan->GetSystemPrincipal(getter_AddRefs(systemPrincipal));
if (NS_FAILED(rv))
...
rv = xh->Init(systemPrincipal, nsnull, nsnull);
if (NS_FAILED(rv))
...

Kirank
 
Posts: 7
Joined: March 24th, 2009, 10:02 pm

Post Posted June 8th, 2009, 7:28 am

Thanks a ton for the time and reply jcrosson. Your reply helped in the sense that now the function send() is returning 0. Means its getting called and executing successfully.
But still I am not getting the expected results out of it. May be there is some problem with my logic. I need to download an exe from the link (or any file for that matter)

rv = pXMLHttpRequest->Open(NS_LITERAL_CSTRING("POST"),NS_LITERAL_CSTRING("http://dl.google.com/googletalk/googletalk-setup-en-GB.exe"));//,false,empty,empty);
pXMLHttpRequest->SetRequestHeader(NS_LITERAL_CSTRING("Content-type"),NS_LITERAL_CSTRING("octet-stream"));
pXMLHttpRequest->OverrideMimeType(NS_LITERAL_CSTRING("octet-stream"));
rv=pXMLHttpRequest->Send(NULL);

through this code. I am not sure about it, do I need to do anything else here?? I am a newbie here and any help will be appreciated a lot.

Thanks in anticipation...

jcrosson
 
Posts: 7
Joined: April 20th, 2009, 2:45 pm

Post Posted June 8th, 2009, 2:46 pm

Binary is tricky (see https://developer.mozilla.org/En/Using_XMLHttpRequest), you may actually have the data and not be able to see it.

Return to Mozilla Development


Who is online

Users browsing this forum: No registered users and 2 guests