B4J Question [RESOLVED] setting http proxy

FabioRome

Member
Licensed User
Longtime 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
Longtime User
Upvote 0

giga

Well-Known Member
Licensed User
Longtime 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.
 
Upvote 0

FabioRome

Member
Licensed User
Longtime 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
 
Upvote 0

FabioRome

Member
Licensed User
Longtime 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
 
Upvote 0

FabioRome

Member
Licensed User
Longtime 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
 
Upvote 0

FabioRome

Member
Licensed User
Longtime 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:
Upvote 0

FabioRome

Member
Licensed User
Longtime 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
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime 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
 
Upvote 0

FabioRome

Member
Licensed User
Longtime 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
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
I know it's an old thread, but this wasn't solved properly it seems.

It's missing the "https" proxy:
B4X:
SetSystemProperty("http.proxyHost", ProxyHost )
SetSystemProperty("https.proxyHost", ProxyHost )
SetSystemProperty("http.proxyPort", ProxyPort )
SetSystemProperty("https.proxyPort", ProxyPort )

That works for me, both for Webview and HttpJobs.

[edit] I guess it's the same for the userName and password
 
Last edited:
Upvote 0
Top