Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HTTP POST
#1
how would this look in au. i cant seem to grasp it.
Function Function6
Trigger F12     Help - how to add the trigger to the macro
Code:
Copy      Help
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)
#2
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.
C# code:
// 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.
C# code:
// 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;

}}
#3
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
#4
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:
C# code:
var jsonstr = r.Content.ReadAsStringAsync().Result;

Or in the first code replace the last code line:
C# code:
    var jsonstr = client.UploadString("http://192.168.1.1/test/", data);

Not tested.
#5
thanks that works.


Forum Jump:


Users browsing this thread: 1 Guest(s)