Posts: 4,225
Threads: 365
Joined: May 2005
I want to write a driver that accesses a site tat requires the user to first login and then moves onto the pages with the detail required.
How do I do this with CML? How do I login first and then call a page after that.
I have asked if there was a way to pass the username and password in the URL but there is not.
Mykel Koblenz
Illawarra Smart Home
Posts: 40,483
Threads: 491
Joined: Aug 2002
What you do is you send the request. It will return you a 401 error. That means, you need to log in to do this. So you then build up an authentication string, which which you put into a header line and send back. Then you send it again.
In most cases, if you already have the information, you can skip the first round trip and just build and send the info the first time and it will take it. Presumably it's Basic or Digest type authentication? If so, there are helper methods on the HTTPClient class to build up the required values to send back in.
Dean Roddey
Explorans limites defectum
Posts: 40,483
Threads: 491
Joined: Aug 2002
BTW, you can set a user name and password on the HTTP client object and then enable auto-authorization and it will handle the 401 response for you. But, you can't get rid of the extra round trip if you do it that way. So it's ok for the occasional HTTP exchange, but no so much if doing ongoing access.
Dean Roddey
Explorans limites defectum
Posts: 4,225
Threads: 365
Joined: May 2005
I'm not sure if I am getting a 401 or not - its redirecting to a login page
This is the base URL
https://myclear.clearnetworks.com.au/ Where I need to logon
Does this match what you are indicating?
Mykel Koblenz
Illawarra Smart Home
Posts: 40,483
Threads: 491
Joined: Aug 2002
In a real browser, in order to get the user name and password from you, it will typically have to response to 401 by going to a login screen, getting that info from you, then re-issuing the request. The browser might do that, or some javascript in the web site might handle it and take you to their own login screen.
When you are doing it yourself via code, you can handle the 401 any way you want, usually by just turning around and feeding in the authorization values based on info you have been configured with.
As I said, in most cases, you can pre-calculate the authorization value and just send it in up front, and avoid the whole extra round trip. That's best if the target server will accept it that way.
Dean Roddey
Explorans limites defectum
Posts: 40,483
Threads: 491
Joined: Aug 2002
10-09-2016, 12:27 PM
(This post was last modified: 10-10-2016, 02:47 PM by Dean Roddey.)
I was going to point you at an existing driver that does this, but off all the ones I know use HTTP, nary a one of them does any authentication. But here's a simple example, where the auth info is being set up front. In this case it's Basic type authentication. For Digest, you'd build the digest type string instead, which is another method.
Code:
Locals=
Card4 BodyBytes(0);
Card4 StatusCode;
URL TarURL;
String RepText;
String ContType;
String AuthString;
Card4 Index;
Card4 Count;
EndLocals;
m_HTTP.CreateBasicAuthStr("someusername", "somepassword", AuthString);
AuthString.Prepend("Basic ");
TarURL.Set4
(
URLProtos.HTTPS
, ""
, ""
, "atargetserver.com"
, 0
, "/info"
, ""
, ""
, m_QParms
);
ContType := "application/json; charset=utf-8";
m_KVPair.Set("Authorization", AuthString);
m_InLines.AddObject(m_KVPair);
// Indicate we have no outgoing body text to send
StatusCode := m_HTTP.SendGET
(
TarURL
, 4000
, "MyAwesomeDriver"
, "application/json; charset=utf-8"
, RepText
, m_OutLines
, ContType
, m_IOBuf
, BodyBytes
, False
, m_InLines
);
Dean Roddey
Explorans limites defectum
Posts: 4,225
Threads: 365
Joined: May 2005
Thanks - I plan to sit down this weekend to have a look at it so will digest it through the week and see what I come up with.
Mykel Koblenz
Illawarra Smart Home