how to use proxy:port for http request in automagic

Post your feature requets for new triggers, conditions, actions and other improvements.

Moderator: Martin

anuraag
Posts: 371
Joined: 24 Jan 2015 02:06

Re: how to use proxy:port for http request in automagic

Post by anuraag » 28 Mar 2020 05:09

I have a script written earlier for http request when Automagic was lacking some functions. I have modified that script to add username, password, proxy. Try this

Code: Select all

/*
url = www.google.com;
proxy = "192.168.43.1";
port = "8080";
username_password = "username:password";
requestMethod = "GET"; //GET, POST, PUT, DELETE etc
requestProperty = newMapFromValues(); //add http headers in key value format
body = json body you want to send

param=newList(httpRequest, url, proxy, port, username_password, requestMethod, requestProperty, body)
eval(param[0]);
*/

httpRequest='url=callJavaConstructor("java.net.URL", "URL(java.lang.String)", param[1]);\nif (param[2]!=null and param[3]!=null) {\n\tproxy = callJavaConstructor("java.net.Proxy", "Proxy(java.net.Proxy$Type, java.net.SocketAddress)", getJavaStaticField("java.net.Proxy$Type", "HTTP"), callJavaConstructor("java.net.InetSocketAddress", "InetSocketAddress(java.lang.String, int)", param[2], param[3]));\n\tconnection=callJavaMethod(url, "java.net.URL", "openConnection(java.net.Proxy)", proxy);\n} else {\n\tconnection=callJavaMethod(url, "java.net.URL", "openConnection()");\n}\nif (param[4]!=null) {\n\tbasicAuth = "Basic " + callJavaStaticMethod("android.util.Base64", "encodeToString(byte[], int)", callJavaMethod(param[4], "java.lang.String", "getBytes()"), getJavaStaticField("android.util.Base64", "NO_WRAP"));\n\tcallJavaMethod(connection, "java.net.URLConnection", "setRequestProperty(java.lang.String, java.lang.String)", "Authorization", basicAuth) \n}\ncallJavaMethod(connection, "java.net.HttpURLConnection", "setRequestMethod(java.lang.String)", param[5]);\nif (isMap(param[6])) {\n\tfor (key in getMapKeys(param[6])) {\n\t\tcallJavaMethod(connection, "java.net.URLConnection", "setRequestProperty(java.lang.String, java.lang.String)", key, param[6][key]);\n\t}\n}\nif (param[7]!=null) {\n\tcallJavaMethod(connection, "java.net.URLConnection", "setDoOutput(boolean)", true);\n\tcallJavaMethod(connection, "java.net.URLConnection", "setRequestProperty(java.lang.String, java.lang.String)", "Content-Length", length("{param[4],jsonformat}"));\n\toutputStream=callJavaMethod(connection, "java.net.URLConnection", "getOutputStream()");\n\tcallJavaMethod(outputStream, "java.io.OutputStream", "write(byte[])", callJavaMethod("{param[4],jsonformat}", "java.lang.String", "getBytes()"));\n\tcallJavaMethod(outputStream, "java.io.OutputStream", "close()");\n}\ncallJavaMethod(connection, "java.net.URLConnection", "connect()");\nresponseCode=callJavaMethod(connection, "java.net.HttpURLConnection", "getResponseCode()");\nheaders=callJavaMethod(connection, "java.net.URLConnection", "getHeaderFields()");\n\nerrorstream=callJavaMethod(connection, "java.net.HttpURLConnection", "getErrorStream()");\nif (errorstream!=null) {\n\treader=callJavaConstructor("java.io.BufferedReader", "BufferedReader(java.io.Reader)", callJavaConstructor("java.io.InputStreamReader", "InputStreamReader(java.io.InputStream)", errorstream));\n\tresponse=callJavaConstructor("java.lang.StringBuffer", "StringBuffer()");\n\tline="";\n\twhile ((line=callJavaMethod(reader, "java.io.BufferedReader", "readLine()"))!=null) {\n\t\tcallJavaMethod(response, "java.lang.StringBuffer", "append(java.lang.CharSequence)", line+"\\n")\n\t}\n} else {\n\tstream=callJavaMethod(connection, "java.net.URLConnection", "getInputStream()");\n\treader=callJavaConstructor("java.io.BufferedReader", "BufferedReader(java.io.Reader)", callJavaConstructor("java.io.InputStreamReader", "InputStreamReader(java.io.InputStream)", stream));\n\tresponse=callJavaConstructor("java.lang.StringBuffer", "StringBuffer()");\n\tline="";\n\twhile ((line=callJavaMethod(reader, "java.io.BufferedReader", "readLine()"))!=null) {\n\t\tcallJavaMethod(response, "java.lang.StringBuffer", "append(java.lang.CharSequence)", line+"\\n")\n\t}\n}'
To run this
Create a script. Paste above code.
Then

Code: Select all

param = newList(httpRequest, "http://www.facebook.com", "168.235.95.186", "41360", "ceJuGc:pdxksg", "GET", null, null);
eval(param[0])
I haven't tested username, password or proxy as i have no use of it.

Limitation of above method is that your other script/expression won't run until above code finishes. So if you are doing for large size file then it's not a good way.

Kendbad
Posts: 20
Joined: 12 Mar 2020 12:02

Re: how to use proxy:port for http request in automagic

Post by Kendbad » 29 Mar 2020 05:24

anuraag wrote:
28 Mar 2020 05:09
I have a script written earlier for http request when Automagic was lacking some functions. I have modified that script to add username, password, proxy. Try this

Code: Select all

/*
url = www.google.com;
proxy = "192.168.43.1";
port = "8080";
username_password = "username:password";
requestMethod = "GET"; //GET, POST, PUT, DELETE etc
requestProperty = newMapFromValues(); //add http headers in key value format
body = json body you want to send

param=newList(httpRequest, url, proxy, port, username_password, requestMethod, requestProperty, body)
eval(param[0]);
*/

httpRequest='url=callJavaConstructor("java.net.URL", "URL(java.lang.String)", param[1]);\nif (param[2]!=null and param[3]!=null) {\n\tproxy = callJavaConstructor("java.net.Proxy", "Proxy(java.net.Proxy$Type, java.net.SocketAddress)", getJavaStaticField("java.net.Proxy$Type", "HTTP"), callJavaConstructor("java.net.InetSocketAddress", "InetSocketAddress(java.lang.String, int)", param[2], param[3]));\n\tconnection=callJavaMethod(url, "java.net.URL", "openConnection(java.net.Proxy)", proxy);\n} else {\n\tconnection=callJavaMethod(url, "java.net.URL", "openConnection()");\n}\nif (param[4]!=null) {\n\tbasicAuth = "Basic " + callJavaStaticMethod("android.util.Base64", "encodeToString(byte[], int)", callJavaMethod(param[4], "java.lang.String", "getBytes()"), getJavaStaticField("android.util.Base64", "NO_WRAP"));\n\tcallJavaMethod(connection, "java.net.URLConnection", "setRequestProperty(java.lang.String, java.lang.String)", "Authorization", basicAuth) \n}\ncallJavaMethod(connection, "java.net.HttpURLConnection", "setRequestMethod(java.lang.String)", param[5]);\nif (isMap(param[6])) {\n\tfor (key in getMapKeys(param[6])) {\n\t\tcallJavaMethod(connection, "java.net.URLConnection", "setRequestProperty(java.lang.String, java.lang.String)", key, param[6][key]);\n\t}\n}\nif (param[7]!=null) {\n\tcallJavaMethod(connection, "java.net.URLConnection", "setDoOutput(boolean)", true);\n\tcallJavaMethod(connection, "java.net.URLConnection", "setRequestProperty(java.lang.String, java.lang.String)", "Content-Length", length("{param[4],jsonformat}"));\n\toutputStream=callJavaMethod(connection, "java.net.URLConnection", "getOutputStream()");\n\tcallJavaMethod(outputStream, "java.io.OutputStream", "write(byte[])", callJavaMethod("{param[4],jsonformat}", "java.lang.String", "getBytes()"));\n\tcallJavaMethod(outputStream, "java.io.OutputStream", "close()");\n}\ncallJavaMethod(connection, "java.net.URLConnection", "connect()");\nresponseCode=callJavaMethod(connection, "java.net.HttpURLConnection", "getResponseCode()");\nheaders=callJavaMethod(connection, "java.net.URLConnection", "getHeaderFields()");\n\nerrorstream=callJavaMethod(connection, "java.net.HttpURLConnection", "getErrorStream()");\nif (errorstream!=null) {\n\treader=callJavaConstructor("java.io.BufferedReader", "BufferedReader(java.io.Reader)", callJavaConstructor("java.io.InputStreamReader", "InputStreamReader(java.io.InputStream)", errorstream));\n\tresponse=callJavaConstructor("java.lang.StringBuffer", "StringBuffer()");\n\tline="";\n\twhile ((line=callJavaMethod(reader, "java.io.BufferedReader", "readLine()"))!=null) {\n\t\tcallJavaMethod(response, "java.lang.StringBuffer", "append(java.lang.CharSequence)", line+"\\n")\n\t}\n} else {\n\tstream=callJavaMethod(connection, "java.net.URLConnection", "getInputStream()");\n\treader=callJavaConstructor("java.io.BufferedReader", "BufferedReader(java.io.Reader)", callJavaConstructor("java.io.InputStreamReader", "InputStreamReader(java.io.InputStream)", stream));\n\tresponse=callJavaConstructor("java.lang.StringBuffer", "StringBuffer()");\n\tline="";\n\twhile ((line=callJavaMethod(reader, "java.io.BufferedReader", "readLine()"))!=null) {\n\t\tcallJavaMethod(response, "java.lang.StringBuffer", "append(java.lang.CharSequence)", line+"\\n")\n\t}\n}'
To run this
Create a script. Paste above code.
Then

Code: Select all

param = newList(httpRequest, "http://www.facebook.com", "168.235.95.186", "41360", "ceJuGc:pdxksg", "GET", null, null);
eval(param[0])
I haven't tested username, password or proxy as i have no use of it.

Limitation of above method is that your other script/expression won't run until above code finishes. So if you are doing for large size file then it's not a good way.
Thanks for your help but I don't know how to use the library.
I only know how to use "action_request_http" available in automagic, to send request method post with headers and body.
I want a feature to go through a flow that is fixed by proxy and all "action request http" on the same flow will use the proxy's ip address.

same as the action "delete http request cookies" in the beta, Clear cookies, connect via proxy, and send requests

Kendbad
Posts: 20
Joined: 12 Mar 2020 12:02

Re: how to use proxy:port for http request in automagic

Post by Kendbad » 01 May 2020 04:42

Desmanto wrote:
21 Mar 2020 17:42
Oh, I forgot we have curl. What is your android version? If you are on latest android, you should have curl binary already in the /system/bin.

You can use that directly as pointed out in your link. Use action execute command, then curl and use the proxy parameter.

But of course this is still a valid request, so we can use the proxy directly in HTTP request action.
I use the latest android, how to get binary in system / bin

Locked