HTTP POST/GET



IBM's Command List programming language & Restructured Extended Executor

HTTP POST/GET

Postby rexxer » Thu Jul 15, 2010 10:28 pm

Hi,

I need to do a form post from my REXX file to another website (for sign on authentication).

. Is there a built-in function for HTTP POST/GET as in PHP and Java?
. If not, how can I accomplish this in Rexx?
. If I use Rexx sockets, can I do a form post from a non-SSL server to a SSL enabled server?
. Are there any other alternatives for HTTP POST/GET other than Rexx sockets?

Thanks guys,
rexxer.
rexxer
 
Posts: 4
Joined: Thu Jul 15, 2010 10:24 pm
Has thanked: 0 time
Been thanked: 0 time

Re: HTTP POST/GET

Postby rexxer » Thu Jul 15, 2010 10:33 pm

forgot to mention that I am using z/OS USS (Unix services) and don't have CURL.
rexxer
 
Posts: 4
Joined: Thu Jul 15, 2010 10:24 pm
Has thanked: 0 time
Been thanked: 0 time

Re: HTTP POST/GET

Postby rexxer » Mon Jul 19, 2010 7:16 pm

Alright guys,

So far I am running a test java application on our z/OS webserver. This application creates a socket on port 4444 and listens on the port. The program seems to compile fun and run.

I then created a test rexx client application on the same server and try to connect to port 4444. So far, I can successfully initialize the socket, create the socket HOWEVER I can't get it to connect to the port 4444 on my local server. Any ideas?

Here is the java application code:

import java.io.*;
import java.net.*;

public class AlienServer
{
public static void main(String args[]) throws IOException
{
Socket sock;
BufferedReader in;
PrintStream out;

ServerSocket servsock = new ServerSocket(4444);

sock=servsock.accept();

out = new PrintStream(sock.getOutputStream());
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));

String msg = in.readLine();
System.out.println(" Received: " + msg);

if (msg.indexOf("hello") > -1)
{
System.out.println(" Friendly contact made");
out.println("Welcome friend");
}
else
{
System.out.println(" Probably an alien");
out.println("ET go home");
}
out.flush();
sock.close();
}
}

And here is the Rexx client application:

/*rexx*/

rc = 0;

fc = SOCKET('Initialize','CLIENT');
parse var fc socket_rc .


if socket_rc <> 0 then
Do;
Call !HTTP_Write 'INITIALIZE failed with return info ' fc;
Exit 200;
End;

Call !HTTP_Write 'Socket initialized';

fc = SOCKET('SOCKET','AF_INET','STREAM','TCP');
parse var fc socket_rc newsocketid

if socket_rc <> 0 then
Do;
Call !HTTP_Write 'SOCKET failed with return info ' fc;
fc = SOCKET('TERMINATE');
Exit 200;
End;

Call !HTTP_Write 'Socket successful';
/* Exit 200; */

serv = SOCKET('GetHostId');

parse var serv servrc ipaddress;

if servrc <> 0 then
Do;
Call !HTTP_Write 'Cannot get local IP Address. rc=' servrc;
Exit 200;
End;

Call !HTTP_Write 'The local IP address is ' ipaddress;

fc = SOCKET('CONNECT',newsocketid,'AF_INET' 4444 ipaddress);
parse var fc connect_rc rest

if connect_rc <> 0 then
Do;
Call !HTTP_Write 'CONNECT failed with return info ' fc;
rc = 99;
Call SHUTDOWN_LABEL;
Exit 200;
End;


Call !HTTP_Write 'CONNECTION ESTABLISED';

fc = SOCKET('SEND',newsocketid,'Hello there','');
parse var fc send_rc num_sent_bytes

if send_rc <> 0 then
Do;
Call !HTTP_Write 'SEND failed with return info ' fc;
rc = 99;
Call SHUTDOWN_LABEL;
Exit 200;
End;

if length(read_string) <> num_sent_bytes then
Do;
Call !HTTP_Write 'number of sbytes does not match number of rbytes';
rc = 99;
Call SHUTDOWN_LABEL;
Exit 200;
End;

fc = SOCKET('READ',newsocketid,'10000');
parse var fc read_rc num_read_bytes received_string

if read_rc <> 0 then
Do;
Call !HTTP_Write 'READ failed with return info ' fc;
rc = 99;
Call SHUTDOWN_LABEL;
Exit 200;
End;

Call !HTTP_Write 'String received: ' recieved_string;

Return 200;

SHUTDOWN_LABEL:
fc = SOCKET('CLOSE',newsocketid);
parse var fc close_rc rest

if close_rc <> 0 then
Do;
Call !HTTP_Write 'CLOSE faield with return info ' fc;
fc = SOCKET('TERMINATE');
End;

return;

/*- !HTTP_Write ------------------------------------------------------+
| Call the HTTP Write service |
+--------------------------------------------------------------------*/
!HTTP_Write:
Parse arg pout
ADDRESS LINKMVS 'IMWXWRT pout'
Return;
rexxer
 
Posts: 4
Joined: Thu Jul 15, 2010 10:24 pm
Has thanked: 0 time
Been thanked: 0 time

Re: HTTP POST/GET

Postby Robert Sample » Mon Jul 19, 2010 7:39 pm

Use the TSO command netstat conn to verify that your program is listening on port 4444. Since this will generate a lot of output, you might want to run the command in a batch TSO job. If the netstat conn indicates your program is listening on port 4444, you 'll probably need to work with your site support group to trace the connection attempt to determine what is going on. If the netstat conn does not show your program listening on port 4444, you are not going to be able to connect to your program. In which case, contact your site support group to determine why the program isn't listening on port 4444.
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: HTTP POST/GET

Postby rexxer » Tue Jul 20, 2010 12:57 am

Robert:

The netstat command is disabled for me on our z/OS.

Well, I read couple of articles, made some changes to the code and now have a working version of the client application. Now I have to figure out how to communicate with a server application residing on different server (different IP address) and running on a secure server (I am using a http server while the server application is on a https server). Any help would be greatly appreciated.

Here is the revised rexx code:

/*rexx*/
/*----------------------------------------------------------------+
| Rexx program to simulate a client application which will talk to|
| the java server application AlienServer. This program showcases |
| the usage of sockets in Rexx. |
| |
+----------------------------------------------------------------*/

rc = 0;

fc = SOCKET('Terminate');

Call !HTTP_Write '<html><head><title>CMS socket try.</title></head>';
Call !HTTP_Write '<body>';

fc = SOCKET('Initialize','CLIENT',2);
parse var fc socket_rc .


if socket_rc <> 0 then
Do;
Call !HTTP_Write 'INITIALIZE failed with return info ' fc '<br />';
Call http_end;
Exit 200;
End;

Call !HTTP_Write 'Socket initialized<br />';

fc = SOCKET('SOCKET',2,'SOCK_STREAM','TCP');
parse var fc socket_rc newsocketid .

if socket_rc <> 0 then
Do;
Call !HTTP_Write 'SOCKET failed with return info ' fc '<br />';
fc = SOCKET('TERMINATE');
Call http_end;
Exit 200;
End;

fc = SOCKET('Socketsetstatus','CLIENT');
Call !HTTP_Write 'Socket status is: ' fc '<br />';

serv = SOCKET('GetHostId');

parse var serv servrc ipaddress;

if servrc <> 0 then
Do;
Call !HTTP_Write 'Cannot get local IP Address. rc=' servrc '<br />';
Call http_end;
Exit 200;
End;

Call !HTTP_Write 'The local IP address is ' ipaddress '<br />';

fc = SOCKET('CONNECT',newsocketid,'AF_INET' 4444 ipaddress);
parse var fc connect_rc rest

if connect_rc <> 0 then
Do;
Call !HTTP_Write 'CONNECT failed with return info ' fc '<br />';
rc = 99;
Call SHUTDOWN_LABEL;
Call http_end;
Exit 200;
End;

CR = D2C(13);

Call !HTTP_Write 'CONNECTION ESTABLISED <br />';

fc = SOCKET('SEND',newsocketid,'hi there friend'||CR);
parse var fc send_rc num_sent_bytes

if send_rc <> 0 then
Do;
Call !HTTP_Write 'SEND failed with return info ' fc '<br />';
rc = 99;
Call SHUTDOWN_LABEL;
Call http_end;
Exit 200;
End;

if length('hi there friend'||CR) <> num_sent_bytes then
Do;
Call !HTTP_Write 'number of sbytes does not match number of rbytes';
Call !HTTP_Write '<br />';
rc = 99;
Call SHUTDOWN_LABEL;
Call http_end;
Exit 200;
End;

received_data = Read_from_socket();

parse var received_data rdata_rc . received_string

if rdata_rc <> 0 then
Do;
Call !HTTP_Write 'string received failed. rdata_rc: ' rdata_rc;
Call !HTTP_Write '<br />';
Call http_end;
Exit 200;
End;

Call !HTTP_Write 'Received string: ' received_string '<br />';
Call SHUTDOWN_LABEL;
Call http_end;

Return 200;

/*- SHUTDOWN_LABEL ---------------------------------+
| Call this function to close the socket. |
+--------------------------------------------------*/

SHUTDOWN_LABEL:

fc = SOCKET('CLOSE',newsocketid);
parse var fc close_rc rest

if close_rc <> 0 then
Do;
Call !HTTP_Write 'CLOSE failed with return info ' fc '<br />';
fc = SOCKET('TERMINATE');
End;

Call !HTTP_Write 'SOCKET TERMINATED <br />';

return;

/*- Peek_at_socket ---------------------------------+
| Call this function to see if more data is to be |
| received from sender |
+--------------------------------------------------*/

Peek_at_socket:

Returned_data = Socket('Recv',newsocketid,1024,'PEEK');
parse var Returned_data s_rc s_type s_port s_ip s_results
parse var Returned_data s_rc s_data_len s_data_text

if s_rc <> 0 then
Do;
Call !HTTP_Write 'Peek at socket failed. fc = ' Returned_data '<br />';
Call http_end;
Exit 200;
End;

Return Returned_data;

/*- Read_from_socket ----------------------------------+
| Call this function to read from the socket (sender) |
+-----------------------------------------------------*/

Read_from_socket:

Response = '';
do forever
Returned_data = peek_at_socket();
If s_rc <> 0 then
Do;
Call !HTTP_Write 'I tried to peek but had a problem: ' s_rc '<br />';
Call !HTTP_Write 'return_data: ' Returned_data '<br />';
Call http_end;
Exit 200;
End;

Response = Response||Returned_data;

If s_data_len = 0 then
Do;
Return Response;
end;
return Response;

/*- http_end ---------------------------------------------------------+
| Function to end the html tags. |
---------------------------------------------------------------------*/

http_end:

Call !HTTP_Write '</body></html>';
Return;

/*- !HTTP_Write ------------------------------------------------------+
| Call the HTTP Write service |
+--------------------------------------------------------------------*/
!HTTP_Write:
Parse arg pout
ADDRESS LINKMVS 'IMWXWRT pout'
Return;
rexxer
 
Posts: 4
Joined: Thu Jul 15, 2010 10:24 pm
Has thanked: 0 time
Been thanked: 0 time


Return to CLIST & REXX