Java erv WebService 的小应用
废话不多说, : :
CityReq.java
패키지 com.weather; import javax.xml.bind.annotation.xmlelement; import javax.xml.bind.annotation.xmlrootelement (@xmlrootelement (name = "getweatherbycityname", namespace = "http://webxml.com.cn/"))); 공개 문자열 getTheCityName () {return thecityName; } @xmlElement (name = "thecityName", namespace = "http://webxml.com.cn/") public void setTheCityName (String theCityName) {this.thecityName = thecityName; }}Weatherwebservicetest.java
패키지 com.weather; import java.io.inputStream; import java.io.outputStream; import java.net.httpurlconnection; import java.net.url; import javax.xml.bind.jaxbcontext; import javavavax.xml.bind.marshaller; import javax.xml.docbax.xml.docbax javax.xml.soap.messagefactory; import javax.xml.xml.soap.soapbody; import javax.xml.soap.soapconstants; import javax.xml.soap.soapenvelope; import javax.xml.xml.soap.soapmessage; public class org.w3c.dom.document; public org.w3c.dom; main (string [] args) {// todo 자동 생성 메소드 스터브 날씨 (); } static void weather () {system.out.println ( "开始登陆 ..."); 문자열 wsdl = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx?wsdl"; System.out.println ( "WSDL :"+WSDL); httpurlconnection urlconn = null; 입력 스트림 ins = null; outputStream ous = null; {url u = new URL (wsdl); urlconn = (httpurlConnection) u.openConnection (); urlconn.setdooutput (true); urlconn.setRequestMethod ( "post"); urlconn.setRequestProperty ( "content-type", "application/soap+xml; charset = utf-8"); //urlconn.setRequestProperty("Content-Type ","text/xml; charset = utf-8 "); // 发送数据 OUS = urlConn.getOutputStream (); 문서 문서 = DocumentBuilDerfactory.newinstance (). newDocumentBuilder (). newDocument (); // Marshaller Marsh = jaxbcontext.newinstance (cityreq.class) .createmarshaller (); CityReq XMLF = New CityReq (); xmlf.setthecityName ( "北京"); //jaxb.marshal(xmlf, new printwriter (System.out)); Marsh.marshal (XMLF, 문서); // soapmessage 对象 soapmessage soapmessage = messagefactory.newinstance (soapconstants.soap_1_2_protocol) .createmessage (); soapbody soapbody = soapmessage.getSoapbody (); soapbody.adddocument (문서); SOAPENVELOPE SOAPENVELOPE = SOAPMESSAGE.GETSOAPPART (). getEnvelope (); soapenvelope.removenamespacedeClaration ( "Env"); soapenvelope.addnamespacedeClaration ( "soap12", "http://www.w3.org/2003/05/soap-envelope"); SOAPENVELOPE.ADDNAMESPACEDECLARATION ( "XSI", "http://www.w3.org/2001/xmlschema-instance"); SOAPENVELOPE.ADDNAMESPACEDECLARATION ( "XSD", "http://www.w3.org/2001/xmlschema"); soapenvelope.setprefix ( "soap12"); SOAPENVELOPE.REMOVECHILD (SOAPENVELOPE.GETHEADER ()); soapbody.setprefix ( "soap12"); // 发送数据 SOAPMESSAGE.WRITETO (OUS); // soapmessage.writeto (System.out); System.out.println (urlconn.getResponseCode ()); System.out.println (urlconn.getResponsEmessage ()); // 接收数据 ins = urlconn.getInputStream (); // 接收的数据需要解组? StringBuffer respmsg = new StringBuffer (); 바이트 [] 바이트 = 새로운 바이트 [1024*1024]; int a = -1; while ((a = ins.read (bytes))! = -1) {respmsg.append (new String (bytes, 0, a)); } system.out.println (respmsg.length ()); System.out.println (Respmsg); // 解组的方式 /* soapmessage responsemessage = messagefactory.newinstance (soapconstants.soap_1_2_protocol) .createmessage (null, ins); unmarshaller undarsh = jaxbcontext.newinstance (cityResp.class) .CreateUnmarShaller (); jaxbelement <CityResp> reponse = unmarsh.unmarshal (responseMessage.getSoapbody (). ExtractContentAsDocument (), cityResp.class); CityResp uresp = reponse.getValue (); System.out.println (uresp.getResult ());*/ ous.close (); ins.close (); urlconn.disconnect (); } catch (예외 e) {e.printstacktrace (); }마지막으로{ } } }感谢阅读 感谢阅读, 希望能帮助到大家, 谢谢大家对本站的支持!