Make HTTP Request from Java extensiom

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

Devops
Posts: 23
Joined: 27 May 2020, 15:23

Make HTTP Request from Java extensiom

Postby Devops » 27 May 2020, 15:42

I want to make an http request to a REST API when a Smartfox Login Request is made.
The response from that API will be used to do some processing in the extension.
I could not find any way to make htto request from JAVA.
Is there any way to do it ?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Make HTTP Request from Java extensiom

Postby Lapo » 28 May 2020, 08:28

Hi,
SmartFoxServer 2X comes with the Apache HTTP Client library, which is a useful set of API for sending HTTP requests to a remote server.
You just need to add the jars to your project:
SFS2X/lib/httpclient-4.5.2.jar
SFS2X/lib/httpcore-4.4.4.jar
(version numbers may be different, based on the version of SFS2X)

Also you can check some examples of usage here:
https://www.vogella.com/tutorials/Apach ... ticle.html

Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
Devops
Posts: 23
Joined: 27 May 2020, 15:23

Re: Make HTTP Request from Java extensiom

Postby Devops » 28 May 2020, 11:18

I have added these 2 libraries in lib folder but I am not sure how do I import them into my extension code.
I tried the following but it gives error
"import org.apache.http.client.HttpClient;"

I would need HttpClient ,HttpGet , HttpResponse ,HttpPost references. Can you please help how do I get these references?

The following is my code which I will be using to see if http request was successful(URL will be changed)
client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://www.vogella.com");
HttpResponse response = client.execute(request);
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Make HTTP Request from Java extensiom

Postby Lapo » 28 May 2020, 13:35

Just to be clear... we're talking about SFS2X, correct?
Because this was posted under SFS PRO 1.x

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
Devops
Posts: 23
Joined: 27 May 2020, 15:23

Re: Make HTTP Request from Java extensiom

Postby Devops » 28 May 2020, 13:47

Oops! Sorry for that.
Since I am new here I didn't check into where should I post. I saw "Server side extension development" and posted it.

Yes I am talking about SFS2X.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Make HTTP Request from Java extensiom

Postby Lapo » 28 May 2020, 14:09

Ok, no problem. I've moved the post in the right section.

Did you check the examples from the article I've linked?

Code: Select all

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://www.vogella.com");
HttpResponse response = client.execute(request);

// Get the response
BufferedReader rd = new BufferedReader
    (new InputStreamReader(
    response.getEntity().getContent()));

String line = "";
while ((line = rd.readLine()) != null) {
    // Parse the response from the server
}


Should be pretty straightforward. In the while loop you can decode the response from the server.
Makes sense?

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
Devops
Posts: 23
Joined: 27 May 2020, 15:23

Re: Make HTTP Request from Java extensiom

Postby Devops » 01 Jun 2020, 07:24

Yes, it is working.
Thanks
Devops
Posts: 23
Joined: 27 May 2020, 15:23

Re: Make HTTP Request from Java extensiom

Postby Devops » 08 Jun 2020, 13:48

I need two ways of making a http request here.

One is I want to wait until request is made and response is recieved.
Second is I want to make an http request and not wait for response.

Is there any way for this?

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Google [Bot] and 57 guests