C # calls the Java interface

Source: Internet
Author: User

Recent work tasks include the interoperability of data between systems, of course, consider the system to open the interface to achieve communication!

As. NET developer, has not yet called the Java interface experience, ashamed ashamed!

Talk not much, go straight to the point!

Call the WebService interface. What is the quickest way to net?

1, of course, add a service reference!

Fill in the WSDL address, a new Xxxxclient object in the code, find the method, pass in the parameter, get the result, very easy!

2. Build the proxy class with svcutil and find it in the Visual Studio tool!

What the? You can't find them? Then you look elsewhere how to add it, hey!

Similarly, enter the WSDL address:

will help you in the default settings of the path to generate a proxy class and a config file, class name, configuration to help you build it! Put it in your project code and cool it up!

But, the most frightening "but" came!

The above two methods call Java WebService There is a serious problem can not solve! Is the method does not support overloading! For example, a login method, someone wrote three different parameters, sorry,. NET is not a tool to differentiate! This method is also not adjustable. Here I can only say that there is a statement of MMP when speaking improper!

B.... But can't it be called? That must be impossible. Surf the internet to find articles, I wipe, everywhere is to write a bunch of, annoying not annoying! I want to use HttpWebRequest, in the internet age, we also want to keep up with the pace of time, do what I use the internet thinking! Ha ha!

1, on the tools!

2, Dot Soap

3, the name casually write, WSDL not much! Fill it up!

4, you see? So many login!. mmp! Double-click this request1

Get this

5, see if it is provided to you the parameters, if so, then congratulations, you find the right!

6, fill in the parameters, adjust one! Hey, it's over! Attention to the right!

7, here comes the point! Dot below the httplog! Wow, the Header and send the newspaper Wenquanyo!

8, this thing is too ugly, I will use my fiddler to simulate it! On the tools!

9, find this tab!

10, press from SOAPUI, over there address, post/get,header,requestbody all copy come! Click Execute to see the left box, return to 200,yes, pass!

11, check this request, see the right inspectors at a glance Ah!

12, well, do so much work, what do you do, directly on the code line? All right, come on!

We'll pack HttpWebRequest's header first.

StringBuilder SB =NewStringBuilder (); sb. Appendline ("<soapenv:envelope xmlns:xsi=\ "http://www.w3.org/2001/xmlschema-instance\" xmlns:xsd=\ "http://www.w3.org/ 2001/xmlschema\ "xmlns:soapenv=\" http://schemas.xmlsoap.org/soap/envelope/\ "xmlns:log=\"/HTTP/ Login.webservice.bos.kingdee.com\ ">"); sb. Appendline ("<soapenv:Header/>"); sb. Appendline ("<soapenv:Body>"); sb. Appendline ("<log:login soapenv:encodingstyle=\ "http://schemas.xmlsoap.org/soap/encoding/\" >"); sb. Appendline ("<username xsi:type=\ "xsd:string\" >mes</userName>"); sb. Appendline ("<password xsi:type=\ "xsd:string\" >mes</password>"); sb. Appendline ("<slnname xsi:type=\ "xsd:string\" >eas</slnName>"); sb. Appendline ("<dcname xsi:type=\ "xsd:string\" >eas20170605</dcName>"); sb. Appendline ("<language xsi:type=\ "xsd:string\" >L2</language>"); sb. Appendline ("<dbtype xsi:type=\ "xsd:int\" >2</dbType>"); sb. Appendline ("</log:login>"); sb. Appendline ("</soapenv:Body>"); sb. Appendline ("</soapenv:Envelope>"); HttpWebRequest req=(HttpWebRequest) httpwebrequest.create (URL); Encoding Encoding=Encoding.UTF8; byte[] bs =Encoding.ASCII.GetBytes (sb.)            ToString ()); stringResponseData =String.Empty; Req. Method="POST"; Req. ContentType="Text/xml;charset=utf-8"; Req. ContentLength=BS.            Length; Req. UserAgent="apache-httpclient/4.1.1 (Java 1.5)"; Req. Headers.add ("accept-encoding","gzip,deflate"); Req. Headers.add ("SOAPAction",""); Cookiecontainer Cookiescontainer=NULL; if(Cookiescontainer = =NULL) {Cookiescontainer=NewCookiecontainer (); }            //req.  Cookiecontainer = Cookiescontainer; //Enable cookies

Re-transmit Requestbody

using (Stream Reqstream = req. GetRequestStream ()) {        0, BS. Length);        Reqstream.close ();}

Then send the request and get the result!

using (HttpWebResponse response = (HttpWebResponse) req. GetResponse ())            {                usingnew  StreamReader (response. GetResponseStream (), encoding))                {                    = reader. ReadToEnd (). ToString ();                }            }             return ResponseData;

OK, the results of how to analyze, I do not have to talk about it, XML has XML tool class, JSON has JSON tool class, C #, Strong invincible!

C # calls the Java interface

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.