B4J Question [RESOLVED] setting http proxy

FabioRome

Member
Licensed User
Hello everyone,
I have a proxy

I declare the following variables:
B4X:
Dim ProxyHost As String
Dim ProxyPort As Int
Dim ProxyUsername As String
Dim ProxyPassword As String
and execute the following instructions:

B4X:
SetSystemProperty("http.proxyHost", ProxyHost )
SetSystemProperty("http.proxyPort", ProxyPort )
SetSystemProperty("http.proxyUser", ProxyUsername )
SetSystemProperty("http.proxyPassword", ProxyPassword )
but i get this error:

B4X:
Program started.
java.net.UnknownHostException: name_external_site
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:901)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1293)
at java.net.InetAddress.getAllByName0(InetAddress.java:1246)
at java.net.InetAddress.getAllByName(InetAddress.java:1162)
at java.net.InetAddress.getAllByName(InetAddress.java:1098)
I tried to replace the name of the proxy with its IP address but does not change anything:
do you have any suggestions?
Thank you.
 

giga

Well-Known Member
Licensed User

giga

Well-Known Member
Licensed User
This sounds like the problem.
result should be: google.com should resolve to an IP
Pinging google.com [216.58.218.206] with 32 bytes of data:
Reply from 216.58.218.206: bytes=32 time=27ms TTL=53
Reply from 216.58.218.206: bytes=32 time=25ms TTL=53
Reply from 216.58.218.206: bytes=32 time=25ms TTL=53
Reply from 216.58.218.206: bytes=32 time=30ms TTL=53

If you are using an IP try to ping it and see if it works.
 

FabioRome

Member
Licensed User
sorry for my english.

PC always uses a proxy so if I do I always get a PING "host not found" but if I go to use the internet browser.

I would be enough to also use the proxy system default but I have the same problem:
B4X:
SetSystemProperty("java.net.useSystemProxies", "True")
with programs written in object-pascal I have no problems with the proxy

Thank you
 

FabioRome

Member
Licensed User
UPDATE

B4X:
java -Dhttp.proxyHost=proxy_ip -Dhttp.proxyPassword=proxy_pwd -Dhttp.proxyPort=8080 -Dhttp.proxyUserName=proxy_user  my_file_name.jar
or

B4X:
java -jar -Djava.net.useSystemProxies=true my_file_name.jar
same mistake
 

FabioRome

Member
Licensed User
B4X:
Private Sub leggi_file_impostazioni_proxy (myProxy As Boolean)

   If myProxy = False Then
     SetSystemProperty("http.proxyHost", "" )
     SetSystemProperty("http.proxyPort", "" )
     SetSystemProperty("http.proxyUser", "" )
     SetSystemProperty("http.proxyPassword", "" )

   Else
     SetSystemProperty("http.proxyHost", "proxy_name" )
     SetSystemProperty("http.proxyPort", "8080" )
     SetSystemProperty("http.proxyUser", "my_username" )
     SetSystemProperty("http.proxyPassword", "my_password" )
   End If
  
End Sub
 

FabioRome

Member
Licensed User
hi Erel,
in attachment you have my example complete.
the file INI is blank:
B4X:
[PROXY]
ProxyHost=xxxx
ProxyPort=8080
ProxySchema=http
ProxyUsername=xxxx
ProxyPassword=xxxx

[DATABASE]
DatabaseHost=localhost
DatabaseName=xxxxx
DatabaseUsername=xxxx
DatabasePassword=xxxx

you have also php file ponte_stetreCHuf_sTeb44ruC-copia.php

Thank you very much
Regards
Fabio
 
Last edited:

FabioRome

Member
Licensed User
modify HttpUtils2Service.bas

B4X:
Public Sub SubmitJob(job As HttpJob) As Int
    '** Modifica
    If Main.ProxyHost <> Null AND Main.ProxyPort <> 0 Then
        If Main.ProxyUsername <> Null AND Main.ProxyPassword <> Null Then
            hc.SetProxy2(Main.ProxyHost, Main.ProxyPort, Main.ProxyScheme, Main.ProxyUsername, Main.ProxyPassword)
        Else
            hc.SetProxy(Main.ProxyHost, Main.ProxyPort, Main.ProxyScheme)
        End If
    End If
    '** Fine Modifica
   
    taskCounter = taskCounter + 1
    TaskIdToJob.Put(taskCounter, job)
    If job.Username <> "" AND job.Password <> "" Then
        hc.ExecuteCredentials(job.GetRequest, taskCounter, job.Username, job.Password)
    Else
        hc.Execute(job.GetRequest, taskCounter)
    End If
    Return taskCounter
End Sub
 

FabioRome

Member
Licensed User
Use HttpUtils2 source code and modify HttpUtils2Service. Set the proxy with hc.SetProxy (or hc.SetProxy2).
Hi @Erel ,
i replaced JHttpUtils2 with JOKHttpUtils and JHttp with JOKHttp.
how can I put the proxy in Jokhttp ?

Thank you
 

Erel

Administrator
Staff member
Licensed User
You can use this code to set a proxy with OkHttp:
B4X:
Sub SetProxy (hc As OkHttpClient, Host As String, Port As Int)
   Dim jo As JavaObject = hc
   Dim proxy, socketAddress As JavaObject
   socketAddress.InitializeNewInstance("java.net.InetSocketAddress", Array (Host, Port))
   proxy.InitializeNewInstance("java.net.Proxy", Array ("HTTP", socketAddress))
   jo.GetFieldJO("client").RunMethod("setProxy", Array(proxy))
End Sub
 

FabioRome

Member
Licensed User
You can use this code to set a proxy with OkHttp:
B4X:
Sub SetProxy (hc As OkHttpClient, Host As String, Port As Int)
   Dim jo As JavaObject = hc
   Dim proxy, socketAddress As JavaObject
   socketAddress.InitializeNewInstance("java.net.InetSocketAddress", Array (Host, Port))
   proxy.InitializeNewInstance("java.net.Proxy", Array ("HTTP", socketAddress))
   jo.GetFieldJO("client").RunMethod("setProxy", Array(proxy))
End Sub
thank you Erel, and for setting also username e password? thank you so much
 
Top