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