1、取汇率
currencyURL.properties:
AMERICAS=http://www.bloomberg.com/markets/currencies/americas_currencies.html ASIA/PACIFIC=http://www.bloomberg.com/markets/currencies/asiapac_currencies.html EUROPE/AFRICA/MIDDLEEAST=http://www.bloomberg.com/markets/currencies/eurafr_currencies.html YAHOO=http://hk.finance.yahoo.com/currency/convert?amt=1&from=USD&to=
代码入口:
GetInternetData getInternetData = new GetInternetData(); try { currencyMaps = getInternetData.getRequestInfo(); } catch (Exception e) { log.error(e.getMessage()); //return mapping.findForward("fail"); }
GetInternetData.java:
读取配置文件:
/** * 从网站读取数据 * @return * @throws Exception */ public final List<Exchangerate> getRequestInfo() throws Exception { //读取配置文件,取得网站路径 ResourceBundle bundle = ResourceBundle.getBundle("currencyURL"); Enumeration<String> enums = bundle.getKeys(); List<Exchangerate> currencyMaps = new ArrayList<Exchangerate>(); while (enums.hasMoreElements()) { String key = enums.nextElement(); //从网站中取得标签 if(!key.equals("YAHOO")){ log.info("==========>"+key); String res = getHttpRequestInfo(bundle.getString(key)); List<Exchangerate> currencyList = getData(res,key); currencyMaps.addAll(currencyList); } } return currencyMaps; }
由URL得到Response:
private final String getHttpRequestInfo(String uri) throws Exception { String response = null; // Create an instance of HttpClient. HttpClient client = new HttpClient(); // Create a method instance. GetMethod method = new GetMethod(uri); // Provide custom retry handler is necessary HttpMethodParams httpMethodParams = method.getParams(); httpMethodParams.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); try { int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) throw new RuntimeException("Httpclient Method failed: " + method.getStatusLine()); response = method.getResponseBodyAsString(); String s1 = "<table"; String e1 = "</table>"; int stratIndex = response.indexOf(s1); int endIndex = response.indexOf(e1, stratIndex) + e1.length(); String currency = response.substring(stratIndex, endIndex); return currency; } finally { method.releaseConnection(); } }
组装数据:
private List<Exchangerate> getData(String response,String urlKey) throws TransformerException{ List<Exchangerate> currencyMaps = new ArrayList<Exchangerate>(); Document doc = null; try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(response))); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 找到table的个数tr NodeList trNodes =doc.getDocumentElement().getElementsByTagName("tr"); log.info(trNodes.getLength()); for(int i=0;i<trNodes.getLength();i++){ Node node = trNodes.item(i); Node theadNode = XPathAPI.selectSingleNode(node, "thead"); if(null==theadNode){ NodeList tdNodes = XPathAPI.selectNodeList(node, "td"); Node tdnode = tdNodes.item(0); System.out.println("tdnodes: " + tdnode); if(null != tdnode) { //if ("CURRENCY".equalsIgnoreCase(tdNodes.item(0).getTextContent().trim())) continue; Exchangerate currency = new Exchangerate(); //将CURRENCY截取为BaseCurrency与TargetCurrency String[] cs = tdNodes.item(0).getTextContent().trim().split("[-]"); currency.setBasecurrency(cs[0]); currency.setSellcurrency(cs[1]); String amountStr = tdNodes.item(1).getTextContent().trim(); amountStr = amountStr.replaceAll(",", ""); currency.setWebExrate(Double.parseDouble(amountStr)); currency.setArea(urlKey); //currency.setUpdatedate(date); currencyMaps.add(currency); } } } return currencyMaps; }
。。。
方式二:
package tags; import java.io.BufferedReader; public class MainTest { /** * @param args */ public static void main(String[] args) { String fromCurrency = "HKD"; String toCurrency = "AUD"; String googleCur = getGoogleCurrency(fromCurrency, toCurrency); String yahooCur = getYahooCurrency(fromCurrency, toCurrency); System.out.println(googleCur+"\n"+yahooCur); } static String getGoogleCurrency(String fromCurrency, String toCurrency){ String rtn = ""; String strGoogleURL = "http://www.google.com/ig/calculator?hl=en&q=1" + fromCurrency + "%3D%3F" + toCurrency; try { // Create a URL for the desired page URL url = new URL(strGoogleURL); // Read all the text returned by the server BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String str; while ((str = in.readLine()) != null) { rtn += str; // str is one line of text; readLine() strips the newline character(s) } in.close(); } catch (MalformedURLException e) { } catch (IOException e) { } return rtn; } static String getYahooCurrency(String fromCurrency, String toCurrency){ String rtn = ""; String strYahooURL = "http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=" + fromCurrency + toCurrency + "=X"; try { // Create a URL for the desired page URL url = new URL(strYahooURL); // Read all the text returned by the server BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String str; while ((str = in.readLine()) != null) { rtn += str; // str is one line of text; readLine() strips the newline character(s) } in.close(); } catch (MalformedURLException e) { } catch (IOException e) { } return rtn; } }
。。。
相关推荐
这个“自己做的一个WebService练习”项目可能是为了帮助初学者理解并实践WebService的基本概念和技术。在这个项目中,你可能会遇到以下几个关键知识点: 1. **SOAP (Simple Object Access Protocol)**:SOAP是用于...
【标题】"第一个Web服务(WebService)例子源码"提供了初学者深入了解和实践WebService开发的绝佳素材。在本文中,我们将深入探讨这个简单的"Hello World"程序如何工作,以及它如何帮助我们理解WebService的核心概念...
详细描述建立一个WebService工程的步骤,包括如果建立、如何部署、如何测试等方法。
第一个webservice例子.pdf第一个webservice例子.pdf
开发一个WebService,你需要选择一个编程语言和框架,如Java的JAX-WS或.NET的ASMX。服务端需要定义接口(对应WSDL),实现业务逻辑,并暴露这些服务供外部调用。在处理请求时,服务端会将接收到的SOAP消息解码,执行...
在本案例中,我们关注的是一个特定的Web服务类型——WebService,它用于获取一个包含泛型对象的List。这个场景在分布式系统、微服务架构或者跨平台数据共享中非常常见。让我们深入探讨一下相关的知识点。 1. **...
开发者可以使用WSDL文档来了解如何调用一个WebService。 4. UDDI(统一描述、发现和集成):UDDI是一个标准的目录服务,用于发布和查找WebService。服务提供商可以在UDDI注册他们的服务,而消费者则可以通过UDDI...
本话题将深入探讨如何通过WebService接口实现一个或多个附件的上传功能。 首先,让我们理解WebService的基本原理。WebService是一个自包含、自描述的应用程序,它可以被其他应用程序通过网络调用,不受平台限制。...
做智能客户端的时候,当用到WebService时,由于服务器处理速度、网络传输速度等各种原因会使一个WebService从请求开始到获得响应结果之间等待一段时间,这时候线程会处于阻塞状态,程序会等待请求结果导致客户端无法...
首先我们创建一个WebService,里面有两个方法:一个无参的方法,一个有参的方法: 创建好了WebService以后,把WebService部署到IIS上,并确保可以访问 二、静态引用 这种方式是通过添加静态引用的方式调用...
WebService接口说明public String getUserInfo(String username,String password)
在这种场景下,Java Axis库是实现WebService的一个常用工具。 Axis是Apache组织开发的一个开源项目,专门用于创建和部署SOAP(简单对象访问协议)服务。SOAP是一种基于XML的协议,用于在Web上交换结构化和类型化的...
下面将详细讲解如何使用MyEclipse创建一个WebService项目服务端,以及涉及的关键知识点。 首先,创建一个新项目是第一步。在MyEclipse中,选择"File" -> "New" -> "Dynamic Web Project",输入项目名称并设置相关...
在ASP.NET环境中,创建一个WebService非常简单。开发者通常会创建一个ASMX文件,其中包含一个或多个公共方法,这些方法可以通过HTTP请求被外部访问。例如,我们可以创建一个名为"HelloWorldService.asmx"的服务,...
在C#中,创建一个Webservice通常涉及以下步骤: 1. 创建一个新的ASP.NET Web Application项目。 2. 添加一个新的Web Service(ASMX)文件,定义服务方法。 3. 在服务方法中实现数据库操作,如使用ADO.NET的DataSet...
本篇将详细介绍Java WebService的入门实例,包括其工作原理以及如何创建和测试一个简单的WebService。 1. **WebService工作原理-SOAP** SOAP(Simple Object Access Protocol)是WebService通信的基础,它是一种...
这里我们关注的是如何使用POST方法调用一个WebService,并处理返回的XML响应。我们将探讨以下知识点: 1. **WebService**:WebService是一种基于HTTP协议的分布式计算模型,允许不同系统间的应用进行交互。它通常...
当客户端首次调用一个WebService时,由于.NET框架的Just-In-Time (JIT)编译机制,服务的代码会被转化为机器码,即动态库,以便运行在特定的硬件上。这个JIT编译过程会消耗一定的时间,特别是在处理复杂的业务逻辑时...
网上写vb6.0调用webservice的都写得比较简单, 这个结合实例作了较详细介绍
在本实例中,我们将深入探讨如何使用C#语言来开发一个基于WebService的操作数据库的应用。 首先,理解WebService的基本原理是至关重要的。WebService基于SOAP(Simple Object Access Protocol)协议,通过HTTP协议...