Send SMS using utl_http
Eric K.Aug 9 2010 — edited Oct 13 2010I am using Oracle Database 10g Enterprise Edition Release 10.2.0.1.0.
I am trying to use this code to send sms. If I paste the URL on the browser, it sends the SMS but when I try to use it with utl_http, I'm getting an error.
----Procedure Starts----
create or replace procedure sendsms (precipient in varchar2, p_sms in varchar2)
is
v_username constant varchar2 (40) := 'username';
v_password constant varchar2 (40) := 'password';
v_request utl_http.req;
v_response utl_http.resp;
v_responsetext varchar2 (2000);
v_error_text varchar2 (200);
v_url varchar2 (2000 char)
:= 'mygateway?uname='
|| v_username
|| '&'
|| 'passwd='
|| 'dest='
|| precipient
|| 'source=XXXXXXX'
|| 'msg='
|| p_sms;
begin
--Set a proxy
utl_http.set_proxy ('proxy:port', 'domain');
dbms_output.put_line (v_url);
-- Send SMS.
v_request := utl_http.begin_request (url => v_url, method => 'POST');
-- Authenticate Proxy
utl_http.set_authentication (r => v_request,
username => 'username',
password => 'password',
scheme => 'Basic',
for_proxy => true
);
utl_http.set_header (r => v_request,
name => 'Content-Type',
value => 'application/x-www-form-urlencoded'
);
utl_http.set_header (r => v_request,
name => 'Content-Length',
value => length (v_url)
);
utl_http.write_text (r => v_request, data => v_url);
v_response := utl_http.get_response (v_request);
dbms_output.put_line ('Response ' || v_response.status_code);
if v_response.status_code = '200'
then
utl_http.read_text (v_response, v_responsetext);
dbms_output.put_line ('Response 2 ' || v_responsetext);
if v_responsetext not like 'Result=OK%'
then
v_error_text := v_responsetext;
end if;
else
v_error_text :=
'HTTP status: '
|| v_response.status_code
|| '-'
|| v_response.reason_phrase;
end if;
--
utl_http.end_response (v_response);
--
if v_error_text is not null
then
raise_application_error (-20001,
'Sending SMS failed with ' || v_error_text
);
end if;
end sendsms;
/
----Procedure Ends------
Error-
ERROR at line 1:
ORA-20001: Sending SMS failed with HTTP status: 400-Bad Request
ORA-06512: at "TEST.SENDSMS", line 67
ORA-06512: at line 1
Please Help anyone?
Thank you.
Edited by: Eric S. on Oct 13, 2010 12:28 PM