URL login with PostData and read responseHeaders

Hi,

I have some experience with hi-level languages but I'm still learning C++ and trying to create an application with JUCE. I'm slowly getting things to work but I'm really missing some simple examples to understand things faster.

In this project I'll be working mostly with HTTP Request, so I'm trying to use the URL library to login to website. I need to do a GET request and the page will return a token in the response (that part works ok), then I need to send a POST request with login data and that token.

I also need to get the Cookies from the Response Headers but I can't figure out how to retrieve the contents from createInputStream(). I'm lost as how to work with StringPairArray*.

I'm sorry, this must be a stupid question but I'm not entirely familiar with the language and I've searched everywhere for an example but couldn't find any. If someone could show me how to login to a website and retrieve the response headers it would help me proceed with my project.

I've made a test program to try to login to this forum but the response text shows that I'm not logged in. Besides my attempts to retrieve the responseHeaders have all failed with a Access violation exceptions, so I must be doing it wrong.

StringPairArray* responseHeaders;
String strHeaders = "Referer: http://www.juce.com/user/login\r\n"
                    "User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)\r\n"
                    "Content-Type: application/x-www-form-urlencoded\r\n";
URL url("http://www.juce.com/user/login");
url = url.withPOSTData("name=Pulover&pass=MyPassword&form_id=user_login&op=Log+in");
url.createInputStream(true, 0, 0, strHeaders, 30000, responseHeaders);
String response = url.readEntireTextStream();
DBG (response); // This returns the same login page, not the logged-in page.

//StringArray values = responseHeaders->getAllKeys(); // This gives me an Access violation exception.

If I could get this to work I think I'd be able to complete the other parts.

Thanks in advance.
 

I found that I need to use readEntireTextStream(true) to read the response as Post, but that alone won't solve my problem (the test above might also be missing another parameter that I could not locate).

I need to read a value from the Cookies in the response headers that will be used as a URL parameter, and what's really puzzling me is why I'm getting access violation when I try StringArray values = responseHeaders->getAllKeys(). What am I doing wrong?

Could someone please show me how I can retrieve the response headers?

Thanks.

Ok, I've found what I did wrong.

StringPairArray* responseHeaders = new StringPairArray();
[...]
StringArray values = responseHeaders->getAllValues();

Just goes to show my lack of knowledge on this language...

You're not going to get very far if you don't understand basic c++ object allocation - you should never allocate StringArrays on the heap. Go and read some C++ tutorials!

Thanks for the tip, Jules. Of course that is just for tests, and I am reading tutorials. I'll make sure to use the proper method in my final project.