In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about software calling C # WebService using Java client classes. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
You don't need to install any third-party tools to use this class, because you use http to send xml files, so you just need to install JDK. By executing this class, you can also get the xml character stream returned by WebServices or xml rpc server, which you can perform other program processing based on the returned xml data. In this way, the data exchange between Java platform and .NET platform is realized and the Java client class calls C # WebService.
The following is the source code SOAPClient4XG.java that satisfies the Java client class call:
/ * SOAPClient4XG. Read the SOAP envelope file passed as the second * parameter, pass it to the SOAP endpoint passed as the first parameter, and * print out the SOAP envelope passed as a response. With help from Michael * Brennan 01 * * @ author Bob DuCharme * @ version 1.1 * @ param SOAPUrl URL of SOAP Endpoint to send request. * @ param xmlFile2Send A file with an XML document of the request. * * 5-23-01 revision: SOAPAction added * / import java.io.*; import java.net.*; public class SOAPClient4XG {public static void main (String [] args) throws Exception {if (args.length < 2) {/ / less than System.err.println ("Usage: java SOAPClient4XG" + "http://soapURL soapEnvelopefile.xml" + "[SOAPAction]"); System.err.println ("SOAPAction is optional."); System.exit (1) } String SOAPUrl = args [0]; String xmlFile2Send = args [1]; String SOAPAction = ""; if (args.length > 2) / / greater than SOAPAction = args [2]; / / Create the connection where we're going to send the file. URL url = new URL (SOAPUrl); URLConnection connection = url.openConnection (); HttpURLConnection httpConn = (HttpURLConnection) connection; / / Open the input file. After we copy it to a byte array, we can see / / how big it is so that we can set the HTTP Cotent-Length / / property. (See complete e-mail below for more on this.) FileInputStream fin = new FileInputStream (xmlFile2Send); ByteArrayOutputStream bout = new ByteArrayOutputStream (); / / Copy the SOAP file to the open connection. Copy (fin,bout); fin.close (); byte [] b = bout.toByteArray (); / / Set the appropriate HTTP parameters. HttpConn.setRequestProperty ("Content-Length", String.valueOf (b.length)); httpConn.setRequestProperty ("Content-Type", "text/xml; charset=utf-8"); httpConn.setRequestProperty ("SOAPAction", SOAPAction); httpConn.setRequestMethod ("POST"); httpConn.setDoOutput (true); httpConn.setDoInput (true); / / Everything's set up; send the XML that was read in to b. OutputStream out = httpConn.getOutputStream (); out.write (b); out.close () / / Read the response and write it to standard out. InputStreamReader isr = new InputStreamReader (httpConn.getInputStream ()); BufferedReader in = new BufferedReader (isr); String inputLine; while ((inputLine = in.readLine ())! = null) System.out.println (inputLine); in.close () } / / copy method from From E.R. Harold's book "Java I OutputStream out O" public static void copy (InputStream in, OutputStream out) throws IOException {/ / do not allow other threads to read from the / / input or write to the output while copying is / / taking place synchronized (in) {synchronized (out) {byte [] buffer = new byte [256]; while (true) {int bytesRead = in.read (buffer); if (bytesRead =-1) break Out.write (buffer, 0, bytesRead);}}
Compilation: javac SOAPClient4XG.java
Running command format: java-classpath. SOAPClient4XG
Http://localhost/BokeServices/Service1.asmx c:loginReq.xml
Http://tempuri.org/UserLoginReq
But instead of running the above command, first introduce the meaning of the command line. Http://localhost/BokeServices/Service1.asmx is the address of C # WebService.
The content in c:loginReq..xml is the Java client class calls WebService method xml file, http://tempuri.org is the WebService method namespace, must have, otherwise the call fails, if you use the method default namespace in C # WebServices, then use http://tempuri.org, otherwise it should be consistent with the definition in C #, UserLoginReq is the method name of C # WebServices. Note that the method name and parameter name in the xml file correspond to the method name and parameter name of C # WebServices one by one (the order of parameters can be reversed).
Let me first introduce a simple example (c:loginReq.xml). This xml file calls the UserLoginReq method of the remote C # WebService with two parameters, UserAcc (username) and UserPwd (password). After a successful call, C# will automatically return a SOAP package in xml format.
xml version= "1.0" encoding= "utf-8"? > < soap:Envelope xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd=" http://www.w3.org/2001/XMLSchema" xmlns:soap= "http://schemas.xmlsoap.org/soap/ envelope/" > < soap:Body > < UserLoginReq xmlns= "http://tempuri.org/" > < UserAcc > baozheng < / UserAcc > < UserPwd > mypwd < / UserPwd > < / UserLoginReq > < / soap:Body > < / soap:Envelope >
Now take a look at the definition of the UserLoginReq method for C # WebServices:
Public struct UserLoginResp {public string UserAcc; public int Result;} [WebMethod] public UserLoginResp UserLoginReq (string UserAcc, string UserPwd,int ReqFrom) {… }
Note that when the structure UserLoginResp,C# WebServices returns SOAP information, the UserLoginResp structure is automatically converted to xml format.
The client using this type of xml rpc server is also very simple. The following is a client-side rpc.xml file that calls the metaWeblog.deletePost method implemented on the xml rpc server side.
< methodCall > < methodName > metaWeblog.deletePost < / methodName > < params > < param > < value > appKeyValue < / value > < / param > < value > 746 < / value > < / param > < param > < value > baozheng < / value > < / param > < param > < value > Hello123 < / value > < / param > < / params > < / methodCall >
Format for invoking the command:
Java-classpath% CLASSPATH%;. SOAPClient4XG.
Http://192.168.25.97:8080/BokeeXmlRpc/xml-rpc rpc.xml
Compared to the command line that calls C # WebServices, you can see that the command line that calls xml rpc server is one less method name argument. Http://192.168.25.97:8080/BokeeXmlRpc/xml-rpc is the servlet address on the server side that provides the xml rpc call.
Thank you for reading! This is the end of this article on "Software uses Java client class to call C # WebService". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.