B4J Question httpjob

jinyistudio

Well-Known Member
Licensed User
Longtime User
Hi

I need login this server first when i typing http://192.168.1.123/di_value/slot_0 in the browser, I cound get respone as following.
B4X:
{"DIVal":[{"Ch":0,"Md":0,"Val":0,"Stat":0,"Cnting":1,"OvLch":0},{"Ch":1,"Md":0,"Val":0,"Stat":0,"Cnting":1,"OvLch":0},{"Ch":2,"Md":0,"Val":0,"Stat":0,"Cnting":1,"OvLch":0},{"Ch":3,"Md":0,"Val":0,"Stat":0,"Cnting":1,"OvLch":0}]}

when use following B4J code, I get error message, Do i need login it first with B4J code !?
B4X:
Dim job1 As HttpJob
    job1.Initialize("dnc",Me)  
    job1.download("http://192.168.1.123/di_value/slot_0")

B4X:
Waiting for debugger to connect...
Program started.
{"Err": 1000, "Msg": ""}
Error: Bad Request
 
Last edited:

jinyistudio

Well-Known Member
Licensed User
Longtime User
Hi
Yes, i first login in the browser. Now,I try following code to send GET Request. but I always get message as following. I don't know How is it !?

B4X:
Sub AppStart (Args() As String)
    Dim job1 As HttpJob
    job1.Initialize("dnc",Me)      
    job1.Download("http://192.168.1.123/di_value/slot_0")
    job1.GetRequest.SetHeader("Authorization","Basic root:00000000")
    job1.GetRequest.SetContentType("application/x-www-form-urlencoded")
    StartMessageLoop
End Sub

B4X:
Waiting for debugger to connect...
Program started.
Error occurred on line: 84 (HttpJob)
java.lang.RuntimeException: Only Post / Put requests support this method.
    at anywheresoftware.b4a.http.HttpClientWrapper$HttpUriRequestWrapper.SetContentType(HttpClientWrapper.java:426)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:656)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:232)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
    at b4j.example.main.main(main.java:29)

following code is from c#. I try trasfer it to b4j.
B4X:
public void SendGETRequest(string account, string password, string URL)
        {
            this.BasicAuthAccount = account;
            this.BasicAuthPassword = password;
            this.URL = URL;
            this.HasData = false;
            this.Method = HttpRequestOption.GET;
            SendRequest();
        }       

        protected void SendRequest()
        {

            HttpWebRequest myRequest;
            System.IO.Stream outputStream;// End the stream request operation

            myRequest = (HttpWebRequest)WebRequest.Create(this.URL); // create request
            myRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(this.BasicAuthAccount + ":" + this.BasicAuthPassword)));
            myRequest.Method = Method.ToString();
            myRequest.KeepAlive = false;
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ReadWriteTimeout = 1000;
            // Create the patch data
            if (this.HasData)//Append data for send
            {
                byte[] byData = Encoding.ASCII.GetBytes(this.JsonifyString); // convert POST data to bytes
                myRequest.ContentLength = byData.Length;
                // Add the post data to the web request
                outputStream = myRequest.GetRequestStream();
                outputStream.Write(byData, 0, byData.Length);
                outputStream.Close();
            }
            try
            {
                myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), myRequest);
            }
            catch (Exception e)
            {
                OnResquestOccurredError(e);
            }
        }
 
Upvote 0

jinyistudio

Well-Known Member
Licensed User
Longtime User
It's OK

B4X:
Sub AppStart (Args() As String)
    Dim SU As StringUtils
    Dim s As String = "root:00000000"
    s = SU.EncodeBase64(s.GetBytes("utf8"))
    Dim job1 As HttpJob
    job1.Initialize("dnc",Me)   
    job1.download("http://192.168.1.123/di_value/slot_0")
    job1.GetRequest.SetHeader("Authorization","Basic "&s)
    StartMessageLoop
End Sub

B4X:
Waiting for debugger to connect...
Program started.
{"DIVal":[{"Ch":0,"Md":0,"Val":0,"Stat":0,"Cnting":1,"OvLch":0},{"Ch":1,"Md":0,"Val":0,"Stat":0,"Cnting":1,"OvLch":0},{"Ch":2,"Md":0,"Val":0,"Stat":0,"Cnting":1,"OvLch":0},{"Ch":3,"Md":0,"Val":0,"Stat":0,"Cnting":1,"OvLch":0}]}
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…