How To Redirect The HTTP Request Activity Through The Proxy?

How to redirect the HTTP Request activity through the proxy?

Issue Description

While trying to use the HTTP Request activity you may receive a NULL, 0, a 407 Response status code, or even an error message as below or if there is a scenario where the HTTP Request needs to be redirected through Proxy.

System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it x.x.x.x:xxxx at System.Net.Sockets.Socket.InternalEndConnect(IAsyncResult asyncResult)

at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)

at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)

--- End of inner exception stack trace ---

at UiPath.Web.Activities.HttpClient.EndExecute(AsyncCodeActivityContext context, IAsyncResult result)

at System.Activities.AsyncCodeActivity.System.Activities.IAsyncCodeActivity.FinishExecution(AsyncCodeActivityContext context, IAsyncResult result)

at System.Activities.AsyncCodeActivity.CompleteAsyncCodeActivityData.CompleteAsyncCodeActivityWorkItem.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)

Root Cause

The HTTP Request activity does not have a section for specifying the proxy details, and because of that, in case a proxy exists on the system, it may not allow the usage of the HTTP Request activity, to perform various Rest API calls and your firewall will block the communication between Client (your Studio/Robot machine) and the Target (the URL that is being tried to access).

Resolution

Assuming there is a proxy server address set in the system where the proxy is specified the addresses to use, ports, and the hostname exceptions (to not go through the proxy).

From Internet Options -> Connections -> LAN settings -> Proxy server -> Advanced

Note: Access Internet Options from the Start menu of your Windows OS or Start and type inetcpl.cpl

In the below example, try to perform a GET request for an endpoint from cloud.uipath.com.

Note: In this example of using UiPath Studio 2023.4.1, with VisualBasic language, Windows - Legacy compatibility, and with UiPath.WebAPI.Activities[1.16.2] dependency.

Adapt the below approach to match the proxy configuration or authentication. For more details, check internally with the Admin/Network or Infrastructure team.

  1. Add a Multiple Assigns activity with the below variables

proxyAddress of type System.String = "your_proxyserver:your_portnumber"

proxyUser of type System.String = "your_username"

ProxyPass of type System.String = "your_password"

bypassHostnames of type System.String[] = { "your_hostname_1", "cloud.uipath.com", "your_hostname_3" }

image.png

  1. Add an Assign activity with the below variable

proxy of type System.Net.WebProxy = New System.net.webProxy(proxyAddress)

image.png

  1. Add an Assign activity with the below variable

This will allow using the Basic authentication (username and password) in cases you are receiving 407 response codes.

proxy.Credentials = new System.net.NetworkCredential(proxyUser, new System.net.NetworkCredential("", proxyPass).SecurePassword)

image.png

  1. Add an Assign activity with the below variable

proxy.BypassProxyOnLocal = True


image.png

  1. Add an Assign activity with the below variable

proxy.BypassList = bypassHostnames


image.png

  1. Add an Assign activity with the below variable

System.net.WebRequest.DefaultWebProxy = proxy

image.png

  1. Add a Try Catch activity

Inside the Try section add the HTTP Request activity

In the Catches select the:

System.Net.WebException as webException

Print this message: "The URL is not accessible." + vbCr + webException.ToString

System.Exception as systemException

Print this message: "Uncaught exception: " + vbCr + systemException.ToString

In the Finally

Print this message: "Status Code: " + statuscode.ToString

  1. The HTTP Request activity may look like the below (adapt it with your details)

Enable SSL certificate verification: True

Timeout (milliseconds): 60000

Accept Format: ANY

Request Method: GET

Request URL: "https://cloud.uipath.com/your_organization_name/your_tenant_name/orchestrator_/odata/Assets"

Headers:

X-UIPATH-FolderPath: "ORCHESTRATOR_FOLDER_NAME"

Authorization: "Bearer your_access_token_value"

Output

Headers: header (create a new variable with Ctrl +k and set it in the variable panel to be as System.Collections.Generic.Dictionary)

Response content: result (create a new variable with Ctrl +k and set it in the variable panel to be as System.String)

Response status: statuscode (create a new variable with Ctrl +k and set it in the variable panel to be as System.Int32)

  1. Add an If activity to check the condition for the response status code and run some specific actions to the True or False events.

For the True condition:

resultHeader = String.Join(Environment.NewLine, header.Select(Function (x) x.Key + ":" + String.Join("|",x.Value)))

For the False condition:

" * Didn't receive a 200 or 201 response status.

* Cannot retrieve the Headers response and the Body response content due to the fact the URL was not accessible.

* Check if the URL is accessible in your default browser. If not, check internally with your network to allow access to it.

* If it is accessible, try to add the URL hostname to your proxy.BypatssList or add your proxy in the workflow.

* Double-check the same Request in the Postman tool. Maybe the provided details are not correct."

Sample results:

Thanks a lot for this how-to, exactely what I needed to set a proxy only valid for a single workflow / http request.