18 June 2007

Character Encoding Problem While Executing Web Services With Oracle


A few months ago, I have writen a post about web services in Oracle. It works funny until one of my colleagues opened me a defect :( The defect was about, utl_http package of oracle, was not able to handle Turkish characters. I walk around the code again I realized that, I have forgetten set Content-Type property in http header of web service. I made a small modification and it runs correctly again. The only thing i did is that setting charset property of http request.
I put down the simple code below:
DECLARE
vs_Request VARCHAR2(32767);
vs_Response VARCHAR2(32767);
http_request utl_http.req;
http_response utl_http.resp;
response_xml xmltype;
vs_Url VARCHAR2(64);
BEGIN
vs_Request := 'some well defined xml data.';
vs_Url := 'http://10.14.194.224/WS';



http_request := utl_http.begin_request(vs_Url, 'POST', 'HTTP/1.0');
utl_http.set_header(http_request, 'Content-Type', 'text/xml;charset=UTF-8');
utl_http.set_header(http_request, 'Content-Length', length(vs_Request));

utl_http.write_text(http_request, vs_Request);

http_response := utl_http.get_response(http_request);

utl_http.read_text(http_response, vs_Response);
utl_http.end_response(http_response);

dbms_output.put_line('Response of request is :');
dbms_output.put_line(substr(vs_Response, 1, 255));
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('Error occured while requesting...');
dbms_output.put_line('Error is ');
dbms_output.put_line(SQLERRM);

END;



But be careful on red line of code. If you put a space such as 'text/xml; charset=UTF-8' you can get error again.

No comments: