Invoke Code Exception - The name 'HttpContext' does not exist in the current context

Hi everyone,

I have the following snippet of C# code:

var client = new RestSharp.RestClient();
client.BaseUrl = new Uri(url);

var request = new RestSharp.RestRequest(RestSharp.Method.POST);
client.FollowRedirects=false;

request.AddCookie("Cookie", "Cookie_1=value; JSESSIONID=node0gi4uhegtzmqevx6bn233abhg344007.node0");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("Payload", payload);

IRestResponse response = client.Execute(request);
cookie = HttpContext.Current.Server.UrlDecode(response.Headers.ToList().Find(x => x.Name == "Set-Cookie").Value.ToString());
Console.WriteLine(cookie);

The output result should be the cookie from the response named “Set-Cookie”. However, when I execute the code it errors out with the exception “The name ‘HttpContext’ does not exist in the current context”.

I have double checked and the namespace “System.Web” is present in the Imported namespaces panel, but the problem persists:
image

Any tips on this case?

In alternative, is there an easier way to do REST API calls where FollowRedirects is set to false? From what I understand, the standard HTTP activity from the Web.API package does not have this functionality.

Thank you.

Have you tested to open your xaml file with Notepad and add <AssemblyReference>System.Web</AssemblyReference> among the list of assemblies?

It’s already present in the file:

image

Does it work if you enter the namespace also?

cookie = System.Web.HttpContext.Current.Server.UrlDecode(response.Headers.ToList().Find(x => x.Name == "Set-Cookie").Value.ToString());

It errors out with this message:

No compiled code to run
error CS0234: The type or namespace name ‘HttpContext’ does not exist in the namespace ‘System.Web’ (are you missing an assembly reference?) At line 12

Can you check:
System.Net.HttpWebRequest
HttpUtility.UrlDecode
For query parameters: HttpUtility.ParseQueryString

Sorry Peter, could you help me to adapt my code with these parameters? I’m not sure of what to change.

Would you mind sharing your workflow? Your code doesn’t generate the same error on my end.

image

Usually when I get the error CS0234 and I know I have imported everything correctly, the workaround is to just declare a variable in the Variables or the Arguments panel. (You don’t need to do anything with the variable.)

Here you go, you’re looking for the file testLogin2.xaml file.

I have 3 arguments defined.

In:
string Url
string payload

Out:
string Cookie

testHttp.zip (98.4 KB)

Hi @andre.f.pires

Though you have imported the namespace, try appending System.Web before Httpcontext

Regards,

No error with your workflow either:

image

Have you tested to declare a variable in the Variables panel or just put the workflow in a new project?

Huh, how strange.

OK, let me create a new project and copy paste the code activity.

So I tried creating a new project in UiPath named “testLogin3”, and now I’m having problems with methods used by the RestSharp library:

I installed the WebAPI packages from UiPath, and imported the RestSharp and System.Web namespaces.

I’m so sorry, but could you please take a look and check what might be wrong?
testlogin3.zip (66.9 KB)

Please copy your original code and paste in your code again in the Invoke Code activity. C# is case sensitive. When you have the language set to VB.NET at first and paste the code, UiPath changes the case automatically (e.g. new to New, false to False). This generates errors even though you change the language to C# later on.

image

Also change Payload to payload and Cookie to cookie.

OK, resolved the code errors and changed names of Cookie to cookie and Payload to payload, but when executing the activity it still errors out with CS0103 :frowning: :

Code

Arguments

Exception
image

Test with this workflow:
Main.xaml (6.0 KB)

Sorry @ptrobot , forgot to add the altered line of code you suggested:

cookie = System.Web.HttpContext.Current.Server.UrlDecode(response.Headers.ToList().Find(x => x.Name == "Set-Cookie").Value.ToString());

It still errors out with CS0234.

Thank you so much @ptrobot, the code is running now, but this expression is throwing an object reference is not set to an instance of an object:

cookie = System.Web.HttpContext.Current.Server.UrlDecode(response.Headers.ToList().Find(x => x.Name == "Set-Cookie").Value.ToString());

How can I make sure that there is content in the response headers?

Nevermind, the expression is correct, I changed the value to another expected response header and it accessed the value no problem, which means that it’s now a problem with the invocation of the request.

You can assign the header to a variable and check if it’s null first. Also I don’t believe you can use HttpContext.Current. It seems to always be null unless you are inside a web application. Microsoft recommends that you use WebUtility.UrlDecode() instead.

            var h = response.Headers.ToList().Find(x => x.Name == "Set-Cookie");
            if (h != null)
            {
                cookie = System.Net.WebUtility.UrlDecode(h.Value.ToString());
            }
            else
            {
                cookie = "No cookie for you!";
            }