Posts: 229
Threads: 22
Joined: Sep 2007
how would this look in au. i cant seem to grasp it.
Function
Function6
Trigger
F12
typelib WinHttp {662901FC-6951-4854-9EB2-D9A2570F2B2E} 5.1
WinHttp.WinHttpRequest xhr._create
xhr.Open("POST" F"http://192.168.1.1/test/")
xhr.setRequestHeader("accept", "application/json")
xhr.setRequestHeader("content-type", "application/json-rpc");
str enc.encrypt(4 "admin:password")
xhr.SetRequestHeader("Authorization", F"Basic {enc}")
str data= "{''jsonrpc'':''1.1'',''method'':''change'',''id'': 0,''params'':[{}]}"
xhr.send(data)
Posts: 12,095
Threads: 142
Joined: Dec 2002
12-10-2019, 10:53 PM
(This post was last modified: 12-10-2019, 11:25 PM by Gintaras.)
Now you can find C# code for tasks like this in stackoverflow and many other places on the internet. It is one of main advantages of QM3.
Script without trigger.
// script "" //.
using Au; using Au.Types; using static Au.AStatic; using System; using System.Collections.Generic;
using System.Net;
using System.Text;
class Script : AScript { [STAThread] static void Main(string[] a) => new Script(a); Script(string[] args) { //;;;
using (var client = new WebClient()) {
client.Headers.Add("accept", "application/json");
client.Headers.Add("content-type", "application/json-rpc");
var enc = Convert.ToBase64String(Encoding.UTF8.GetBytes("admin:password"));
client.Headers.Add("Authorization", $"Basic {enc}");
var data = "{''jsonrpc'':''1.1'',''method'':''change'',''id'': 0,''params'':[{}]}";
client.UploadString("http://192.168.1.1/test/", data);
}
}}
The same with the newer class HttpClient. May be slower. Both codes not tested.
// script "" //.
using Au; using Au.Types; using static Au.AStatic; using System; using System.Collections.Generic;
using System.Net.Http;
using System.Text;
class Script : AScript { [STAThread] static void Main(string[] a) => new Script(a); Script(string[] args) { //;;;
using var client = new HttpClient();
var h = client.DefaultRequestHeaders;
h.Add("accept", "application/json");
h.Add("content-type", "application/json-rpc");
var enc = Convert.ToBase64String(Encoding.UTF8.GetBytes("admin:password"));
h.Add("Authorization", $"Basic {enc}");
var data = "{''jsonrpc'':''1.1'',''method'':''change'',''id'': 0,''params'':[{}]}";
var r = client.PostAsync("http://192.168.1.1/test/", new StringContent(data)).Result;
}}
Posts: 229
Threads: 22
Joined: Sep 2007
why do i get Encoding.UTF8.GetBytes not exist in current context.
i cut short in first post about how to read the response.
str jsonstr=xhr.ResponseText how do i archive this.
its early days for me and C# copy and paste does not always run as expected
Posts: 12,095
Threads: 142
Joined: Dec 2002
12-11-2019, 12:03 AM
(This post was last modified: 12-11-2019, 12:11 AM by Gintaras.)
Maybe copied not whole script. Some code is hidden between the visible first and second lines. Click the Copy link.
When in the second code you have r:
var jsonstr = r.Content.ReadAsStringAsync().Result;
Or in the first code replace the last code line:
var jsonstr = client.UploadString("http://192.168.1.1/test/", data);
Not tested.
Posts: 229
Threads: 22
Joined: Sep 2007