Java calls PHP's Web Service (iii)

Source: Internet
Author: User
Tags soap soap client vars xml parser wsdl

Usoap is an open source Soap tool in a PHP environment, which is a much more used tool.

In the UTF-8 environment, NUSOAP can work very well. However, when used in Chinese environment, Nusoap often have some problems that people can not solve.

In a recent project, the server was implemented with NUSOAP, supporting UTF-8 and GBK two character sets.

     An error occurred when the client invoked the service with GBK: Charset from HTTP Content-type Us-ascii does not match encoding from XML declaration GBK, meaning that in the client's request, the HTTP Content-type character set is Us-ascii, and in the XML declaration of the SOAP request, the character set is GBK, and the two do not match. Check the request variable of the SOAP client, the value of HTTP Content-type is also GBK, how can it become us-ascii? It's a little confusing. So had to trace the source of Nusoap, found that nusoap in the processing of HTTP Content-type when the Us-ascii,iso-8859-1,utf-8 character set is the default to Us-ascii. The reason for this is that NUSOAP uses XML parser, and XML parser only supports these kinds of character sets. Therefore, when the client is called, the character set of the HTTP Content-type and the SOAP request should be replaced with iso-8859-1 when the GBK is used.

Later, when you encapsulate the client, you also encounter a similar problem. The client character set is declared as GBK, and the service side returns the SOAP call result when both HTTP Content-type and SOAP request declare the character set to GBK and the client does not get any value. View the response object of the SOAP client and find that the server is returned correctly. To solve this problem, we had to modify the server side to declare both the HTTP Content-type and the SOAP response character set as Iso-8859-1.

So when using Nusoap, you can use ISO-8859-1 instead when you encounter the GBK or GB2312 character set.

=============================================================================================

PHP Web Service Server side:

  1. <?php
  2. Header ("Content-type:text/html;charset=utf-8");
  3. Pull in the Nusoap code
  4. Require_once ('./lib/nusoap.php ');
  5. Define the method as a PHP function
  6. function Hello ($name) {
  7. return ' Hello! ‘ .  $name;
  8. }
  9. Create the server instance
  10. $server = new Soap_server;
  11. $server->configurewsdl (' hellowsdl ', ' urn:hellowsdl ');
  12. $server->wsdl->schematargetnamespace = ' urn:hellowsdl ';
  13. Register the method to expose
  14. $server->register (' Hello ',
  15. Array (' name ' = 'xsd:string '),
  16. Array (' return ' = 'xsd:string '),
  17. ' Urn:hellowsdl ',
  18. ' Urn:hellowsdl#hello ',
  19. ' RPC ',
  20. ' Encoded ',
  21. ' Say Hello to Somebody '
  22. );
  23. Use the request to invoke the service
  24. $HTTP _raw_post_data = isset ($HTTP _raw_post_data)?  $HTTP _raw_post_data: ";
  25. $server->service ($HTTP _raw_post_data);
  26. ?>

Client side:

  1. <?php
  2. Header ("content-type:text/html;charset=gb2312");
  3. Pull in the Nusoap code
  4. Require_once ('./lib/nusoap.php ');
  5. Create the Client instance
  6. $client = new SoapClient (' http://localhost/soapTest/helloService.php?wsdl ', true);
  7. Call the SOAP method
  8. $param = Array ("name" = "Andy");
  9. $result = $client->call (' Hello ', $param);
  10. Display the result
  11. Print_r ($result);
  12. if (! $err =$client->geterror ()) {
  13. Print_r ($result);
  14. Print (' </br> ');
  15. echo "program returns:", Htmlentities ($result, ent_quotes,gb2312);
  16. }
  17. else{
  18. echo "error:", Htmlentities ($result, ent_quotes,gb2312);
  19. }
  20. echo ' $client, request, ent_quotes,gb2312).   ' </pre> ';
  21. echo ' $client, Response, ent_quotes,gb2312).   ' </pre> ';
  22. echo ' $client, Debug_str, ent_quotes,gb2312).   ' </pre> ';
  23. ?>

Java code:

Note: To use axis1.x, go to the official website do not download the Axis2. It seems that axis1.x and Axis2 are still very different, and the current axis1.x documents compare all points. These are the online searches.

If you need to invoke the Web Service using the Chinese parameter, you must use the ISO-8859-1 encoding parameter, and the returned response is decoded again. Do not use another encoding, there will be errors!

Java code

...

Java code

  1. Import Org.apache.axis.client.Service;
  2. <span style="color: #464646; Font-family:simsun; line-height:21px; Text-align:left; White-space:normal; Background-color: #ffffff; " >import org.apache.axis.client.call;</span>
  3. Public class WebServiceTest {
  4. public static void Main (string[] args) {
  5. String endpoint = "http://localhost/soapTest/helloService.php";
  6. //string endpoint = "http://testweb.dev.php/testWebService/testWebService.php";//This paragraph is the address just above
  7. Service service = new service ();
  8. Call call;
  9. try {
  10. Call = (call) Service.createcall ();
  11. Call.settargetendpointaddress (new Java.net.URL (endpoint));
  12. Call.setoperationname ("Hello");
  13. string param = new String ("Andy". GetBytes (),"iso-8859-1"); If this paragraph is not added, the Chinese parameter will be garbled
  14. //string param = new String ("Chinese");
  15. string s = (string) call.invoke (new object[] {param});
  16. s = new String (S.getbytes ("iso-8859-1")); If there is no conversion code, Chinese will be garbled
  17. System.out.println (s);
  18. } catch (Exception e) {
  19. //TODO auto-generated catch block
  20. E.printstacktrace ();
  21. }
  22. }
  23. }

Java calls PHP's Web Service (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.