Connectivity between Java and. Net WebService

Source: Internet
Author: User
Tags call back

Recently, I was busy working on a project with foreign companies. The web service provided by foreign companies is not my. NET platform. After a long time, I found many problems. In summary,

First, I got the WSDL files provided abroad. I used Microsoft wsdl.exe to generate the CS code I needed. In this way, I used wsdl.exe to call foreign web services ,:

After opening the Command Prompt window:

In this way, we can call foreign web services, but please note:

The code generated by Microsoft has many more attributes than the methods defined in the WSDL. For personal understanding, Let's explain:

1. An asynchronous call method is provided for each method. For example, if the name of a method is createorder, begincreateorder, endcreateorder, createorderasync, and oncreateorderoperationcompleted are added.

For detailed usage and instructions, refer to the description of Microsoft msdn.

2. the specific parameters are Enum and special date types. Microsoft automatically adds the bool field with the specified suffix for us, such as the enum type flag. The added name is flagspecified, remember to set this value to true before it can be non. net web service. otherwise, the soap package will not have a value.

Tools for tracking soap packets can be used, such as tcptrace.

Clients outside China need to call back the. NET web service environment provided by me, but foreign companies report an error:

 <? XML version = "1.0" encoding = "UTF-8"?>

-<Soap: envelope xmlns: Soap ="Http://schemas.xmlsoap.org/soap/envelope/"Xmlns: xsi ="Http://www.w3.org/2001/XMLSchema-instance"Xmlns: XSD ="Http://www.w3.org/2001/XMLSchema">

-<Soap: Body>

-<Soap: Fault>

 <Faultcode>Soap: Client</Faultcode>

 <Faultstring>Server did not recognize the value of HTTP header soapaction :.</Faultstring>

 <Detail/>

 </Soap: Fault>

 </Soap: Body>

 </Soap: envelope>

I found some information on the Internet and put it here. Today I need to verify whether it is feasible:

Recently I have been working on a project that involves web-service calls. I have been studying for a week. Now I am just a summary. Which of the following is to pass Xi Logistics System (http://vip.cxcod.com/PodApi/GetPodStr.asmx? (Web-service call.

1. Use axis1.x to call the WebService Method

The most common versions of axis: 1.4 and 2.0. Version 1.4

Core code:

// Webserviceurl

Service_url = "http://vip.cxcod.com/PodApi/GetPodStr.asmx? WSDL ";

Service = new service ();

Call call = (CALL) service. createcall ();

Call. settargetendpointaddress (New java.net. URL (service_url ));

// Set the method to be called

// Http://intelink.net/is the targetnamespaceattribute value of the definitionsnode in wsdl.

Call. setoperationname (New QNAME ("http://intelink.net/", "getstrbyjobno "));

// Parameters required for this method

Call. addparameter ("custno ",

Org. Apache. axis. encoding. xmltype. xsd_string,

Javax. xml. rpc. parametermode. In );

Call. addparameter ("passwd ",

Org. Apache. axis. encoding. xmltype. xsd_string,

Javax. xml. rpc. parametermode. In );

Call. addparameter ("jobno ",

Org. Apache. axis. encoding. xmltype. xsd_string,

Javax. xml. rpc. parametermode. In );

// Return Value Type of the Method

Call. setreturntype (Org. Apache. axis. encoding. xmltype. xsd_string );

// Call. setusesoapaction (true); // call. setsoapactionuri ("http://intelink.net/GetStrByJobno ");

// Call this method. New object [] {custno, passwd, jobno} is the parameter list.

String xmlstr = call. Invoke (new object [] {custno, passwd, jobno}). tostring ();

} Catch (exception e ){

E. printstacktrace ();

}

Java calls WebService in this way. Note the following:

1. The server fails to recognize the value of the HTTP header soapaction:

Symptom 1:

Web Service + ASP.. NET applications are deployed in the default server directory, and HTTP is used in IE: // <server address>/<program Directory Name>/<default startup page Name> the error "the server fails to recognize the value of the HTTP header soapaction" occurs.

Symptom 2:

When the. NET web service is called on the Java platform, "the server fails to recognize the value of the HTTP header soapaction" appears ".

Symptom 3:

The. NET web service is called on the Java platform, and data is unavailable sometimes.

Solution:

Add the property [soapdocumentservice (routingstyle = soapserviceroutingstyle. requestelement)] To the. NET WebService class (that is, the class under the. asmx file).

TIPS:

What is soapaction? What does it do in WSDL?

The soapaction HTTP request header is used to identify the destination of a soap HTTP request. Its value is a URI address. Soap sending does not limit the format, Uri features, or it must be parsed. In this case, when sending an http soap request, its HTTP client must use/to specify the soapaction HTTP request header.

The content of the soapaction header can be used on the server, for example, the firewall filters HTTP-based SOAP request messages. The soapaction header value is an empty string (""), indicating that the SOAP Message destination is identified by the HTTP request URI. If there is no value, it indicates that the Message destination is not specified.

I add:

When you call. Net WebService In the. NET environment, the following error occurs: "the value of soapaction is not unique in all methods of XML Web Services." can also be solved through this method.

2. To allow Java to call WebService methods. Every webserviced method written by netp must be declared as the RPC method, that is, add "[soaprpcmethod...]".

Example: [webmethod]

[Soaprpcmethod (use = soapbindinguse. literal, Action = http://tempuri.org/HelloWorld ", requestnamespace =" http://tempuri.org/", responsenamespace =" http://tempuri.org/")]

3. Processing of return values and parameters:

Try to write the return values and parameters of the WebService method as strings instead of complex object types, so that the data can be transmitted over the network. This avoids the difficulty of converting complex object types... If the return type is string array type, you can set the return type to org. Apache. axis. encoding. xmltype. soap_vector or Java. Lang. String []. Class.

2. Use xfire to call WebService

Xfire is a new generation of Java Web service engine. xfire makes it easy to publish Web Services in Java EE applications. Compared with other Web service engines, xfire is easy to configure and can be easily integrated with spring, which allows Java developers to achieve the same development efficiency as. NET developers.

Core code:

Service = new objectservicefactory (). Create (iwebservice. Class );

Xfireproxyfactory factory =

New xfireproxyfactory (xfirefactory. newinstance (). getxfire ());

String url = "http: // localhost: 8080/WebServices/services/WebServices ";

Iwebservice IW = (iwebservice) Factory. Create (Service, URL );

List list = IW. gettest ();

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.