title: How to Set SOCKS proxy for webview
body: <p>I need to set webview to use SOCKS proxy. I have been researching this issue from days now. I see that webview gives no public interface to set PROXY. There have been examples that use Java reflection to set http proxy.</p>
  
  <p>as described here :</p>
  
  <p><a href="https://stackoverflow.com/questions/4488338/webview-android-proxy">WebView android proxy</a></p>
  
  <p>But this discussion is about setting http/https proxy.</p>
  
  <p>For setting socks proxy I used the following approach :</p>
  
  <pre><code>private static boolean setProxyKKPlus(WebView webView, String host, int port) {
          Log.d(TAG, "Setting proxy with &gt;= 4.4 API.");
  
          Context appContext = webView.getContext().getApplicationContext();
          String user = NativeSDK.getProxyUser();
          String pass = NativeSDK.getProxyPass();
  
          System.setProperty("socksProxyHost", host);
          System.setProperty("socksProxyPort", port + "");
          System.setProperty("java.net.socks.username", user);
          System.setProperty("java.net.socks.password", pass);
          Log.d(TAG,"socks proxy set");
  
  
  
          try{
              Class applictionCls = Class.forName("android.app.Application");
              Field loadedApkField = applictionCls.getField("mLoadedApk");
              loadedApkField.setAccessible(true);
              Object loadedApk = loadedApkField.get(appContext);
              Class loadedApkCls = Class.forName("android.app.LoadedApk");
              Field receiversField = loadedApkCls.getDeclaredField("mReceivers");
              receiversField.setAccessible(true);
              ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk);
              for (Object receiverMap : receivers.values()) {
                  for (Object rec : ((ArrayMap) receiverMap).keySet()) {
                      Class clazz = rec.getClass();
                      if (clazz.getName().contains("ProxyChangeListener")) {
  
                          Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class);
                          Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
                          onReceiveMethod.invoke(rec, appContext, intent);
                      }
                  }
              }
  
              Log.d(TAG, "Setting proxy with &gt;= 4.4 API successful!");
              return true;
          }
  
      catch (all exceptions) {
  
          Log.d(TAG, "Setting proxy with &gt;= 4.4 API NOT successful!");
          return false;
      }
  </code></pre>
  
  <p>But this does not seem to really work :
  I get this error in my Logs from ProxyChangeListener :</p>
  
  <pre><code>E/ProxyChangeListener: Using no proxy configuration due to exception:java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.get(java.lang.String)' on a null object reference
  </code></pre>
  
  <p>Now the strange part is : after this code when I enter the url in webview, the request is read by the socket listening on the port above - the proxy port. ( I believe that indicates that the request is being proxied) But the request that I read from the socket looks like : \x05\x01</p>
  
  <p>Now as I understand: (From <a href="https://en.wikipedia.org/wiki/SOCKS#SOCKS5" rel="nofollow noreferrer">wiki</a>)</p>
  
  <p>The initial greeting from the client is</p>
  
  <p>field 1: SOCKS version number (must be 0x05 for this version)
  field 2: number of authentication methods supported, 1 byte
  field 3: authentication methods, variable length, 1 byte per method supported</p>
  
  <p>So is this SOCKS5 client request.
  But then where is the info about authentication methods ?</p>
  
  <p>Can someone help me setting SOCKS proxy for webview</p>