HomeGenie Forum

Automation Program Plugins and Wizard Scripting => Help => Topic started by: Jens on July 17, 2016, 07:56:43 PM

Title: Two jason requests in a row?
Post by: Jens on July 17, 2016, 07:56:43 PM
Hello,

I am struggling with jason requests, with the first request I have to authenticate

http://IPADRESS/manager?action=login&username=user&secret=password&format=json&jsoncallback=? (http://IPADRESS/manager?action=login&username=user&secret=password&format=json&jsoncallback=?)

the second request would allow me to send messages

http://IPADRESS/manager?action=setnewmessage&number=620&account=1&content=testmessage&flag=1&format=json&jsoncallback=? (http://IPADRESS/manager?action=setnewmessage&number=620&account=1&content=testmessage&flag=1&format=json&jsoncallback=?)

If I use a web browser and enter the first command i am getting a success response, entering the second command afterwards in the very same browser window, I am getting also a success response.

I tried to transfer that to homegenie by using Net.Webservice, but I am always getting a authentication required by issuing the second request.

Any Idea on how to get the commands beeing sent that way that they rely on the same session (I think this is the nature of the problem)?

Many thanks
Regards

Jens
Title: Re: Two jason requests in a row?
Post by: [email protected] on July 20, 2016, 04:46:48 PM
Is it using cookies or headers for an auth tokens? might be worth trying with fiddler (windows app) or similar browser debugging to see if it is.

You then may need to create a cookiecontainer to use with your request, or specifically add the required header.

David
Title: Re: Two jason requests in a row?
Post by: Jens on July 20, 2016, 08:16:47 PM
Hi David,

thanks for picking this up and you are right, I did a wireshark trace, a cockie is beeing used.

Please see the 200ok.jpg, this is the reply from the login request and the nextgetrequest.jpg contains the cockie received by the first login.

Do you have any sample code which you could share which stores that cockie? I am not a c# expert at all but can get my things done once I have a good starting point

Again, many thanks
regards
Jens

Title: Re: Two jason requests in a row?
Post by: [email protected] on July 24, 2016, 12:49:32 AM
I'll see if I can find something Monday
Title: Re: Two jason requests in a row?
Post by: [email protected] on July 26, 2016, 01:06:48 PM
I dont have anything similar, quick google returns this:

https://www.stevefenton.co.uk/2012/10/automating-web-login-with-httpwebrequest/ (https://www.stevefenton.co.uk/2012/10/automating-web-login-with-httpwebrequest/)

and possibly this (once you ditch the view state and eventvalidation):

http://stackoverflow.com/questions/7198005/c-sharp-httpwebrequest-website-sign-in (http://stackoverflow.com/questions/7198005/c-sharp-httpwebrequest-website-sign-in)

I would personally dev this in visual studio first (without using statements) so it works in homegenie - be easier :)

Title: Re: Two jason requests in a row?
Post by: Jens on July 31, 2016, 05:32:25 PM
I tried it with the following code

CookieContainer myContainer = new CookieContainer();
HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://firstrequest");
request.CookieContainer = myContainer;
HttpWebResponse response = (HttpWebResponse) request.GetResponse();

Program.Notify ("cookiecounter", Convert.ToString(response.Cookies.Count));

The request returns 4 cookies (visible in the wireshark screenshot), but cookiecounter tells me 1, it
equals to the last entry (Max-Age=900)?

Any further idea?


Thanks
Jens
 
Title: Re: Two jason requests in a row?
Post by: [email protected] on August 01, 2016, 10:41:23 AM
what properties are available on myContainer
Title: Re: Two jason requests in a row?
Post by: Jens on August 01, 2016, 12:07:56 PM
Initially none, then I added these two

CookieContainer myContainer = new CookieContainer {
    PerDomainCapacity = 5,
    Capacity =5
};

But that does noct change anything Program.Notify ("cookiecounter", Convert.ToString(response.Cookies.Count)); still says 1 (also if I pick a different webpage for the webrequest.


If I manually add a cookie, CooKies.Count increases to two.
myContainer.Add(new Cookie("admin","1","/","IPADDRESS"));

So I think the problem is getting the cookies out of the first response, this code only shows me only the last cookie, too.

string cookiesresult = "";
 foreach (Cookie cook in response.Cookies)       
 {           
   cookiesresult = cook.Value; 
   Program.Notify ("cockie", cookiesresult);

}
Title: Re: Two jason requests in a row?
Post by: Jens on August 02, 2016, 12:09:35 PM
I think the problem has something to do with the cookie type

http://stackoverflow.com/questions/4248672/httpwebrequest-and-set-cookie-header-in-response-not-parsed-wp7 (http://stackoverflow.com/questions/4248672/httpwebrequest-and-set-cookie-header-in-response-not-parsed-wp7)

Title: Re: Two jason requests in a row?
Post by: Jens on August 02, 2016, 06:34:50 PM
Solution found

I was still on mono 3.2.x, an upgrade to 4.2.2 fixed the issue, now all cookies are transferred and count shows 4

Title: Re: Two jason requests in a row?
Post by: [email protected] on August 03, 2016, 12:14:31 AM
Cool