views:

119

answers:

4

I'm using a web-service from a provider who is being a little too helpful in anticipating my needs. They have given me a HTML snippet to paste on my website, for users to click on to trigger their services. I'd prefer to script this process, so I've got a php script which posts a cURL request to the same url, as appropriate. However, this provider is keeping tabs on my session, and interprets each new request as an update of the first one, rather than each being a unique request.

I've contacted the provider regarding my issue, and they've gone so far as to inform me that their system is working as intended, and that it's impossible for me to avoid using the same ASP.NET session for each subsequent cURL request. While my favored option would be to switch to a different vendor, that doesn't appear to be an option right now. Is there a reliable way to get a new ASP.NET session with each cURL request?

I've tried the following set of CURLOPT's, to no avail:

//initialize curl 
$ch = curl_init($url); 

//build a string out of the post_vars
$post_str = http_build_query($post_vars);

//set the necessary curl options 
curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FAILONERROR, 1); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str); 
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "UZ_".uniqid());
curl_setopt($ch, CURLOPT_REFERER, CURRENT_SITE_URL."index.php?newsession=".uniqid());
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Pragma: no-cache", "Cache-Control: no-cache"));

//execute the call to the backend script, retrieve the results 
$xmlstr = curl_exec($ch);
A: 

Well given the options you are using, it seems you have covered your basics. Can you find out how their sessions are setup?

If you know how they setup a session, IE what they use (if it is IP or what not) and then you can figure out a work around. Another option is trying to set the cookies in a different cookie file:

CURLOPT_COOKIEFILE - The name of the file containing the cookie data. The cookie file can be in Netscape format, or just plain HTTP-style headers dumped into a file.

But if all they do is check cookies your current code should work. If you can figure out what the cookie's name is, you can pass a custom cookie that is blank with the request to see if that works. But if you can get information out of them on how their session's work, that would be best.

Brad F Jacobs
A: 

use these two line to handle the session:

`curl_setopt($ch, CURLOPT_COOKIEJAR, "path/to/cookies.txt"); // cookies.txt should be writable
curl_setopt($ch, CURLOPT_COOKIEFILE, "path/to/cookies.txt");`
Nurul Ferdous
+1  A: 

Normal Asp.net session is tracked by a cookie called ASP.NET_SessionId. This cookie is sent within the response to your first request. So as long as your curl requests don't send back this asp.net cookie, each of your requests will have no connection to each other. Use the curl -c option to see what cookies are flying in-between you and them. Overriding this cookie with a cookie file should work if you confirm that it is normal asp.net session being used here.

It is quite poor for a service to use session (http has much cleaner ways of maintaining state which ReST exploits) so I wouldn't completely rule out the vendor switch option.

llevera
After examining my cookies, it would appear that I was doing everything correctly from the get-go. I'm receiving a different ASP.NET_SessionId every time. I'm pushing for the vendor switch. These guys are a nightmare to work with.
Andrew Rueckert
+1  A: 

If cURL isn't helping much, why not try other methods to call the services from your script, like php's file() function, or file_get_contents().

If you see do not see any difference at all, then the service provider might be using your ip to track your requests ... try uding some proxy for a test ...

Samnan
After further testing, it would appear that they're not actually using ASP_NET's sessions, which is bizarre, since I'm also fairly certain that they're not using my IP. If I enter the GET parameters into the location bar of a Chrome browser and a Chrome (privacy mode) browser, it gives me two different sessions. If I enter the GET parameters in a browser, clear all of my cookies and cache files, and enter them again in the same browser, my session persists. I have no idea how they managed THAT trick...
Andrew Rueckert
I guess it looks that they are creating session based on the get parameters and the browsers identification string. Which is why it maybe creating different sessions on same paramters in different browsers. You can try using command line curl to simulate this behavious and figure out if this is the case.
Samnan