Java calls the. NET ASMX service

Source: Internet
Author: User

Sometimes, under the Java development will invoke the. NET write service, look at the online there are various methods, but always unsuccessful, summed up under their own test can call the success of the way:

1. Original Mode Http-soap

 Public StaticString Postws ()throwsIOException {OutputStreamWriter out=NULL; StringBuilder stotalstring=NewStringBuilder (); String Soapstr=Gensoapxml (); Try{URL urltemp=NewURL ("Http://10.6.54.238:8005/SendMessageService.asmx"); HttpURLConnection Connection=(HttpURLConnection) urltemp.openconnection (); Connection.setdooutput (true); Connection.setrequestmethod ("POST"); //connection.setrequestproperty ("Host", "10.6.54.238");Connection.setrequestproperty ("Content-type", "text/xml; Charset=utf-8 "); //connection.setrequestproperty ("Content-length", Integer.tostring (Soapstr.length ()));Connection.setrequestproperty ("SOAPAction", "Http://ilink.***.com.cn/sendmessage"); out=NewOutputStreamWriter (Connection.getoutputstream (), "UTF-8");            Out.write (SOAPSTR);            Out.flush ();            String Scurrentline; InputStream L_urlstream=Connection.getinputstream (); BufferedReader L_reader=NewBufferedReader (NewInputStreamReader (L_urlstream));  while((Scurrentline = L_reader.readline ())! =NULL) {stotalstring.append (scurrentline); }        } finally {            if(NULL!=Out ) {                Try{out.close (); } Catch(IOException e) {e.printstacktrace (); }            }        }        returnstotalstring.tostring ();}StaticString Gensoapxml () {/*String str= "<?xml version=\" 1.0\ "encoding=\" utf-8\ "? >\n" + "<soap12:envelope xmlns:xsi=\" http://www.w3.org/2001/XMLSchema-instance\ "Xmlns:xsd=\"Http://www.w3.org/2001/XMLSchema\ "Xmlns:soap12=\"Http://www.w3.org/2003/05/soap-envelope\ ">\n" + "<soap12:body>\n" + "<sendmessage xmlns=\"http://ilink.***.com.cn/\ ">\n" + "<strrequest></strrequest>\n" + "</sendmessage>\ N "+" </soap12:body>\n "+" </soap12:Envelope> ";*/StringBuilder SB=NewStringBuilder (); Sb.append ("<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>"); Sb.append ("<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/\ ">" ); Sb.append ("<soap:Body>"); Sb.append ("<sendmessage xmlns=\ "http://ilink.***.com.cn/\" >"); Sb.append ("<strrequest><! [cdata["+genreq () +"]]></strrequest>" ); Sb.append ("</SendMessage>" ); Sb.append ("</soap:Body>" ); Sb.append ("</soap:Envelope>"); returnsb.tostring (); }

Red Italic part is called method and parameter

2. Use Apache axis to look elegant

Add maven Dependency

<dependency> <groupId>org.apache.axis</groupId>   <artifactId>axis</artifactId>   <version>1.4</version></dependency>        <dependency>           <groupId> org.apache.axis</groupid>           <artifactId>axis-jaxrpc</artifactId>            <version>1.4 </version>        </dependency>        <dependency>            <groupId>wsdl4j</groupId>            <artifactId>wsdl4j</artifactId>            <version>1.6.3</version>        </dependency>        <dependency>            <groupId>commons-discovery</groupId>            <artifactId> commons-discovery</artifactid>            <version>0.5</version>        </dependency>

 Public Static voidTestasmx ()throwsremoteexception, Serviceexception, malformedurlexception {Map<string, string> params =NewHashmap<string, string>(); Params.put ("Strrequest", Genreq ()); String result= Callasmxwebservice ("Http://10.6.54.238:8005/SendMessageService.asmx", "http://ilink.***.com.cn/", "SendMessage", params); Logger.error ("ASMX:" +result); }    Staticstring Callasmxwebservice (String serviceurl, String servicenamespace, String methodName, Map<string, string> params)throwsserviceexception, Malformedurlexception, remoteexception {org.apache.axis.client.Service Service=NewOrg.apache.axis.client.Service (); Call Call=(call) Service.createcall (); Call.settargetendpointaddress (NewJava.net.URL (serviceurl)); Call.setoperationname (NewQName (servicenamespace,methodname)); ArrayList<String> paramvalues =NewArraylist<string>();  for(Map.entry<string, string>Entry:params.entrySet ()) {Call.addparameter (NewQName (Servicenamespace, Entry.getkey ()), xmltype.xsd_string, Javax.xml.rpc.ParameterMode.IN);        Paramvalues.add (Entry.getvalue ());        } call.setreturntype (xmltype.xsd_string); Call.setusesoapaction (true); Call.setsoapactionuri (Servicenamespace+methodName); return(String) Call.invoke (Newobject[] {paramvalues.toarray ()}); }

3. Use Httpclient4.5.jar, also post soap XML

 Public StaticString Testws ()throwsIOException {String serviceurl= "Http://10.6.54.238:8005/SendMessageService.asmx";
HttpClient Client=Newdefaulthttpclient (); HttpPost HttpPost=NewHttpPost (serviceurl); String Str=Gensoapxml (); Httppost.setentity (NewStringentity (str, "text/xml;"), HTTP. Utf_8)); HttpResponse HttpResponse=Client.execute (HttpPost); intCode=httpresponse.getstatusline (). Getstatuscode (); if (==code) {httpentity entity=httpresponse.getentity (); InputStream InputStream=entity.getcontent (); byte[] Contentbytes =Ioutils.tobytearray (InputStream); String Res=NewString (Contentbytes, HTTP. UTF_8);
Inputstream.close ();
returnRes;
}
return null; }

Java calls the. NET ASMX service

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.