主要方法是:通过程序中设置代理用公司内网访问外部Webservice

public InputStream getSoapInputStream(String url) {
InputStream inputStream = null;
try {
URL urlObj = new URL(url);
InetSocketAddress addr = new InetSocketAddress("dailifuwuqiIP", duankou);//此处为代理服务器地址,端口
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
HttpURLConnection urlConn = (HttpURLConnection) urlObj
.openConnection(proxy);
urlConn.setRequestProperty("Host", SERVICES_HOST); // 具体webService相关
urlConn.setRequestMethod("GET");
urlConn.connect();
inputStream = urlConn.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return inputStream;
}

类文件:

Weather.java

package weather;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException; public class weather {
// private static String SERVICES_HOST = "www.webxml.com.cn";
private static String SERVICES_HOST = "webservice.webxml.com.cn"; /**
* 城市代码 / 浦东: 2015
*/
private static int CITICODE = ; public static void main(String[] args) throws Exception {
String time=null;
Calendar cal=Calendar.getInstance();
SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd");//MM要大写
time=format.format(cal.getTime()); String desc = "今天是" + time+",浦东";
desc += new weather().getWeatherStr();
System.out.println(desc);
} /**
* Sos,MMyTest
* */
public InputStream getSoapInputStream(String url) {
InputStream inputStream = null;
try {
URL urlObj = new URL(url);
InetSocketAddress addr = new InetSocketAddress("XXX.XX.XX.XX", XXXX);//此处为代理服务器,和端口
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
HttpURLConnection urlConn = (HttpURLConnection) urlObj
.openConnection(proxy);
urlConn.setRequestProperty("Host", SERVICES_HOST); // 具体webService相关
urlConn.setRequestMethod("GET");
urlConn.connect();
inputStream = urlConn.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return inputStream;
} public String getWeatherStr() {
String desc = "";
try {
List<String> weatherList = getWeather(CITICODE); if (weatherList != null && weatherList.size() > ) {
String tianqi = weatherList.get();
if (tianqi.contains("日")) {
tianqi = tianqi.substring(tianqi.indexOf("日") + );
}
String wendu = weatherList.get();
desc += ",天气" + tianqi;
desc += " ,";
desc += wendu.replace("℃", "度").replace("/", "--");
}
} catch (Exception e) {
e.printStackTrace();
return desc;
}
return desc;
} public List<String> getWeather(int cityCode) {
List<String> weatherList = new ArrayList<String>();
Document document;
DocumentBuilderFactory documentBF = DocumentBuilderFactory
.newInstance();
documentBF.setNamespaceAware(true);
try {
DocumentBuilder documentB = documentBF.newDocumentBuilder();
InputStream inputStream = getSoapInputStream("http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather?theUserID=&theCityCode=2015");
document = documentB.parse(inputStream);
NodeList nl = document.getElementsByTagName("string");
int len = nl.getLength();
for (int i = ; i < len; i++) {
Node n = nl.item(i);
String weather = n.getFirstChild().getNodeValue();
weatherList.add(weather);
}
inputStream.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (DOMException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return weatherList;
}
}

运行weather.java文件后打印出:

今天是2014-04-30,浦东,天气 晴转多云 ,14度--24度

学习Webservice之入天气小试的更多相关文章

  1. Java中Webservice调用.NET天气接口生成客户端异常

    学习webservice时候有个例子调用公网的天气预报接口实现查询天气的功能.然而在使用命令编译客户端代码的时候出错了.大概看了一下网上说是需要将将文件中所有出现的 < s:element re ...

  2. 学习 WebService 第三步:一个简单的实例(RAD+WAS 8.5开发SOAP项目)

    [开发环境] Web Service 服务器端开发工具:RAD(Eclipse内核) Web Service 服务器:IBM WebSphere v8.5 REST/SOAP:SOAP(JAX-WS/ ...

  3. WebService初入

    WebService 1. WebService基本认识 WebService最早是微软提出了一种以XML为载体网络信息传输的规范,现在几乎所有的语言与平台都支持,带有状态机制,不依赖于容器,可以发送 ...

  4. webserive学习记录4-获取天气的例子

    学习到了如何创建使用webservice服务,下面就实际应用一下,从网络上获取天气数据. 先从网络上找到免费的webservice服务, 如这个网站:http://www.webxml.com.cn/ ...

  5. curl通过调用WebService查询当前天气

    <?php /** * curl通过调用WebService查询北京的当前天气 */ header("Content-type: text/html; charset=utf-8&qu ...

  6. Spring AOP 自定义注解获取http接口及WebService接口入参和出参

    注解方法实现过程中可以采用如下获取方式:—以下为例  HttpServletRequest request = ((ServletRequestAttributes) RequestContextHo ...

  7. 学习 WebService 第五步:在Local创建测试用WebService(WSDL)

    [准备] Eclipse+Tomcat7(Tomcat端口修改为不冲突的值) axis2 1.7.7 jar包(没有来这里下载:http://www.apache.org/dyn/closer.lua ...

  8. c++学习笔记01-输入输出流&&文件输入输出操作

    const int size=50等于#define size 50 字符串默认以0结尾: 一.输入输出流 cin.get()会从输入中取走一个字符: cin.peek()会从输入中检测第一个字符: ...

  9. C语言学习笔记frist---输入两个数比较大小

    C#学习中,问道艰辛,今自C学起,第一个函数学习:输入两个数比较大小,仅作练习: #include "stdafx.h" #include<stdio.h> // 包含 ...

随机推荐

  1. du命令 实现Linux 某个文件夹下的文件按大小排序

    1. df -lh 2. du -s /usr/* | sort -rn这是按字节排序 3. du -sh /usr/* | sort -rn这是按兆(M)来排序 4.选出排在前面的10个du -s ...

  2. eclipse项目里面的类有时候会莫名其妙出现很多错误

    由于eclipse的编译是基于时间戳的判断机制的.因此当你按build   all的时候有些eclipse认为时间戳没有改变的类不会被编译.因此你可以先clean一下再编译.这个时候eclipse会将 ...

  3. R提高篇(一): 输入输出

    目录: 文本输出 图形输出 数据输入 数据框输出 文本导入 Excel导入 文本输出 语法: sink(file = NULL, append = FALSE, type = c("outp ...

  4. SQL SERVER 组内排序

    取出每组的第一个 select *from (select * ,RANK ( ) OVER( PARTITION by org order by reportcode asc) PartionNum ...

  5. centos 安装sphinx

    官网下载 :http://sphinxsearch.com/downloads/sphinx-2.2.10-release.tar.gz/thankyou.html 安装sphinx 解压 tar z ...

  6. Neutron分析(7)—— neutron-l3-agent HA solutions

    1. keepalived vrrp/conntrackd High availability features will be implemented as extensions or driver ...

  7. SPOJ #440. The Turtle´s Shortest Path

    Coding a Dijkstra is not hard. %70 of my time spent on tackling TLE, as my last post. Dijkstra works ...

  8. Django的列表反序

    Django虽然是python的web框架,但它不是所有的python特性都支持的. 最近在项目中遇到一个问题,需要在Django中将获得的列表反序排列,一开始我使用的是python的reverse方 ...

  9. python学习-day02

    ---恢复内容开始--- 一.pycharm安装 1.1破解方式http://www.cnblogs.com/evlon/p/4934705.html 1.2.头部配置: 二.运算符 2.1.比较运算 ...

  10. Linux下查看和添加环境变量

    转自:http://blog.sina.com.cn/s/blog_688077cf01013qrk.html $PATH:决定了shell将到哪些目录中寻找命令或程序,PATH的值是一系列目录,当您 ...