more G-Labs products

Author Topic: Two jason requests in a row?  (Read 1852 times)

July 17, 2016, 07:56:43 PM
Read 1852 times

Jens

  • *****
  • Information
  • Global Moderator
  • Posts: 211
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=?

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=?

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

July 20, 2016, 04:46:48 PM
Reply #1

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271
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

July 20, 2016, 08:16:47 PM
Reply #2

Jens

  • *****
  • Information
  • Global Moderator
  • Posts: 211
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


July 24, 2016, 12:49:32 AM
Reply #3

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271
I'll see if I can find something Monday

July 26, 2016, 01:06:48 PM
Reply #4

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271
I dont have anything similar, quick google returns this:

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

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


July 31, 2016, 05:32:25 PM
Reply #5

Jens

  • *****
  • Information
  • Global Moderator
  • Posts: 211
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
 

August 01, 2016, 10:41:23 AM
Reply #6

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271
what properties are available on myContainer

August 01, 2016, 12:07:56 PM
Reply #7

Jens

  • *****
  • Information
  • Global Moderator
  • Posts: 211
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);

}
« Last Edit: August 01, 2016, 12:17:59 PM by Jens »

August 02, 2016, 12:09:35 PM
Reply #8

Jens

  • *****
  • Information
  • Global Moderator
  • Posts: 211

August 02, 2016, 06:34:50 PM
Reply #9

Jens

  • *****
  • Information
  • Global Moderator
  • Posts: 211
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


August 03, 2016, 12:14:31 AM
Reply #10

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271