B4J Question [RESOLVED] setting http proxy

Discussion in 'B4J Questions' started by FabioRome, Apr 22, 2015.

  1. FabioRome

    FabioRome Member Licensed User

    Hello everyone,
    I have a proxy

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

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

    Code:
    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.
     
  2. giga

    giga Well-Known Member Licensed User

  3. FabioRome

    FabioRome Member Licensed User

    console: ping google.com (no www)
    "host not found"
     
  4. giga

    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.
     
  5. FabioRome

    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:
    Code:
    SetSystemProperty("java.net.useSystemProxies""True")
    with programs written in object-pascal I have no problems with the proxy

    Thank you
     
  6. FabioRome

    FabioRome Member Licensed User

    UPDATE

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

    Code:
    java -jar -Djava.net.useSystemProxies=true my_file_name.jar
    same mistake
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Can you post the code that raises this exception?
     
  8. FabioRome

    FabioRome Member Licensed User

    Code:
    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
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    This code by itself will not cause any error. Where is the code that makes the network call?
     
  10. FabioRome

    FabioRome Member Licensed User

    hi Erel,
    in attachment you have my example complete.
    the file INI is blank:
    Code:
    [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: Apr 24, 2015
  11. Erel

    Erel Administrator Staff Member Licensed User

    Use HttpUtils2 source code and modify HttpUtils2Service. Set the proxy with hc.SetProxy (or hc.SetProxy2).
     
    FabioRome likes this.
  12. FabioRome

    FabioRome Member Licensed User

    thank you very much
     
  13. FabioRome

    FabioRome Member Licensed User

    modify HttpUtils2Service.bas

    Code:
    Public Sub SubmitJob(job As HttpJobAs 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
     
  14. FabioRome

    FabioRome Member Licensed User

    Hi @Erel ,
    i replaced JHttpUtils2 with JOKHttpUtils and JHttp with JOKHttp.
    how can I put the proxy in Jokhttp ?

    Thank you
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    You can use this code to set a proxy with OkHttp:
    Code:
    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
     
    mindful and FabioRome like this.
  16. FabioRome

    FabioRome Member Licensed User

    thank you Erel, and for setting also username e password? thank you so much
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    It is currently not possible to set username and password for the proxy.
     
    FabioRome likes this.
  18. FabioRome

    FabioRome Member Licensed User

    thanks anyway
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice