Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE
Highlighted
brendang
New Member.
405 views

HttpClient convert code please

 Hi

We are using VC 2.3 for VS 2015.  We have a Windows Forms application that uses HttpClient, etc, to send and receive SOAP messages.

But I read from Microsoft:

"HttpClient is intended to be instantiated once and re-used throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. This will result in SocketException errors. Below is an example using HttpClient correctly."

C#
public class GoodController : ApiController
{
    // OK
    private static readonly HttpClient HttpClient;

    static GoodController()
    {
        HttpClient = new HttpClient();
    }
}

So can somebody convert the above code to Visual Cobol please?  The application consists of a main Form that calls other Forms and classes, all in separate projects.  Should we just instantiate the class at the main Form and reference it in the necessary projects?

Thank you

Brendan

0 Likes
5 Replies
Micro Focus Expert
Micro Focus Expert

RE: HttpClient convert code please

I'd do it this way:

class-id GoodController inherits type ApiController.

1 httpClient type HttpClient public property as "HttpClient" with no set.

method-id new static.
set httpClient to new HttpClient
end method new.

Then use GoodController::HttpClient in your other classes. Untested, but aside from any accidental syntax errors that should work.

The key is that in managed COBOL, you define a class constructor as a static new method.

0 Likes
brendang
New Member.

RE: HttpClient convert code please

Hi Michael

Thanks for your answer.

I am getting the error COBCH0969 Cannot access object data from a static method on the line [:(]:

set httpClient to new HttpClient

Also, the ApiController part in the original code threw me a bit, it refers to ASP and we are not using it, so I am just saying:

class-id GoodController...

 

Thanks

Brendan

0 Likes
Micro Focus Expert
Micro Focus Expert

RE: HttpClient convert code please

I forgot static on the declaration of httpClient:

1 httpClient type HttpClient static public property as "HttpClient" with no set.

The static keyword makes it a class member, rather than an instance member, which is what you want - just one copy of it in the program.

I only had it inheriting from ApiController because that's what your C# example had. It's not necessary.

0 Likes
brendang
New Member.

RE: HttpClient convert code please

Hi Michael

Thanks.

Really stupid question probably, if the httpClient is instantiated in FormA, and FormA calls FormB, and FormB calls FormC, and FormC calls the web service, do we have to pass httpClient from FormA to FormB to FormC? Or is there a better way? And do we pass by reference each time? Still trying to get the hang of this sort of thing.

Thanks

Brendan
0 Likes
Micro Focus Expert
Micro Focus Expert

RE: HttpClient convert code please

No - it's a public, static property of FormA, so you can just refer to FormA::HttpClient in the other classes.

Static properties exist for the lifetime of the process (or from when the class is loaded, if it's explicitly loaded dynamically while the process is running), and static constructors are invoked at process startup (or when the class is loaded). So FormA::HttpClient will always refer to the single, process-lifetime HttpClient object, and can be used in any class in the same assembly as the FormA class, and in any class built with a reference to that assembly.

So in FormC you'd just do something like set resultTask to type FormA::HttpClient::GetAsync(url). (I don't know exactly how you're using HttpClient, but that's how you'd refer to it.)

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.