⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gettemp.java

📁 《JAVA WEB服务应用开发详解》代码.zip
💻 JAVA
字号:
   package samples.xmethods;
   
   import java.io.*;
   import java.util.*;
   import java.net.*;
   import org.w3c.dom.*;
   import org.xml.sax.*;
   import javax.xml.parsers.*;
   import org.apache.soap.util.xml.*;
   import org.apache.soap.*;
   import org.apache.soap.encoding.*;
   import org.apache.soap.encoding.soapenc.*;
   import org.apache.soap.rpc.*;
   import org.apache.soap.transport.http.SOAPHTTPConnection;
   
   /**
    * Client to speak to XMethods weather reporting service at
    * http://www.xmethods.com/detail.html?id=8
    *
    * @author Wouter Cloetens (wouter@mind.be)
    */
   public class GetTemp {
       public static final String DEFAULT_SERVICE_URL =
           "http://services.xmethods.net/soap/servlet/rpcrouter";
   
       public static void main(String[] args) throws Exception {
           String serviceURL = null;
           String proxyHost = null, proxyUserName = null, proxyPassword = null;
           int proxyPort = -1;
           String zipcode = null;
   
           // parse command line arguments
           boolean goodUsage = false;
           for (int i = 0; i < args.length; i++) {
               if ("-p".equalsIgnoreCase(args[i])) {
                   goodUsage = false;
                   if (args.length > i) {
                       i++;
                       int pos = args[i].indexOf(':');
                       if (pos != -1) {
                           try {
                               proxyPort =
                                   Integer.parseInt(args[i].substring(pos + 1));
                               proxyHost = args[i].substring(0, pos);
                               goodUsage = true;
                           } catch(NumberFormatException nfe) {
                           }
                       }
                   }
               } else if ("-a".equalsIgnoreCase(args[i])) {
                   goodUsage = false;
                   if (args.length > i) {
                       i++;
                       int pos = args[i].indexOf(':');
                       if (pos != -1) {
                           goodUsage = true;
                           proxyUserName = args[i].substring(0, pos);
                           proxyPassword = args[i].substring(pos + 1);
                       } else
                           break;
                   } else
                       break;
               } else if ("-u".equalsIgnoreCase(args[i])) {
                   goodUsage = false;
                   if (args.length > i && serviceURL == null) {
                       i++;
                       serviceURL = args[i];
                       goodUsage = true;
                   } else
                       break;
               } else if (zipcode == null) {
                   goodUsage = true;
                   zipcode = args[i];
               } else {
                   goodUsage = false;
                   break;
               }
           }
   
           if (!goodUsage) {
               System.err.println("Usage: java samples.xmethods.GetTemp " +
                                  "[-u service-URL] " +
                                  "[-p <HTTP proxy hostname:port>] " +
                                  "[-a <HTTP proxy username:password>] " +
                                  "zipcode");
               System.exit(1);
           }
   
           if (serviceURL == null)
               serviceURL = DEFAULT_SERVICE_URL;
           URL url = new URL(serviceURL);
   
           // create the transport and set parameters
           SOAPHTTPConnection st = new SOAPHTTPConnection();
           if (proxyHost != null) {
               st.setProxyHost(proxyHost);
               st.setProxyPort(proxyPort);
   
               if (proxyUserName != null) {
                   st.setProxyUserName(proxyUserName);
                   st.setProxyPassword(proxyPassword);
               }
           }
   
           // build the call.
           Call call = new Call();
           call.setSOAPTransport(st);
           call.setTargetObjectURI("urn:xmethods-Temperature");
           call.setMethodName("getTemp");
           call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
           Vector params = new Vector();
           params.addElement(new Parameter("zipcode", String.class,
                                           zipcode, null));
           call.setParams(params);
   
           // invoke it
           System.err.println("Invoking weather service at: ");
           System.err.println("\t" + serviceURL);
           Response resp;
           try {
                resp = call.invoke(url, null);
        
           } catch(SOAPException e) {
               System.err.println("Caught SOAPException (" +
                                  e.getFaultCode () + "): " +
                                  e.getMessage ());
               return;
           }
   
           // check response
           if (!resp.generatedFault()) {
               Parameter ret = resp.getReturnValue();
               Object value = ret.getValue();
   
               System.out.println("The temperature is " + value +
                                  " degrees Fahrenheit.");
           } else {
               Fault fault = resp.getFault();
               System.err.println("Generated fault: ");
               System.out.println("  Fault Code   = " + fault.getFaultCode());
               System.out.println("  Fault String = " + fault.getFaultString());
           }
       }
   }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -