


import com.gyf.weather.WeatherInterface;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import java.util.List;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
//使用CXF方式调用
//1.创建式bean
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
//2.设置接口
factoryBean.setServiceClass(WeatherInterface.class);
//3.设置的地址
factoryBean.setAddress("http://127.0.0.1:12345/weather?wsdl");
//4.创建服务
WeatherInterface ws = factoryBean.create(WeatherInterface.class);
List<String > cities = ws.getCityNameByProvince("广西");
System.out.println(ws.queryWeather("广州"));
System.out.println(cities);
}
}
import com.gyf.weather.WeatherInterfaceImpl;
import com.gyf.weather.WeatherInterfaceImplService;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class Main2 {
public static void main(String[] args) throws Exception {
//发送webservice的网络基于soap网络请求
/**
* HttpURLConnection
*/
//1.创建URL【访问路径】
URL url = new URL("http://127.0.0.1:12345/weather");
//2.创建连接
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
//3.设置请求头
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("content-type","text/xml; charset=utf-8");
//4.设置请求参数
httpConn.setDoOutput(true);//可以写数据
OutputStream os = httpConn.getOutputStream();
os.write(paramtersStr("广州").getBytes());
//5.接收服务器响应数据
InputStream is = httpConn.getInputStream();
byte[] buf = new byte[1024];
int len = 0;
while ((len = is.read(buf)) != -1){
//把字节转成字符串
System.out.println(new String(buf,0,len,"utf-8"));
}
//6.关流
is.close();
os.close();
}
public static String paramtersStr(String cityName){
return "<?xml version=\"1.0\" ?>\n" +
"<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
"\t<S:Body>\n" +
"\t\t<ns2:queryWeather xmlns:ns2=\"http://weather.gyf.com/\">\n" +
"\t\t\t<arg0>"+ cityName+"</arg0>\n" +
"\t\t</ns2:queryWeather>\n" +
"\t</S:Body>\n" +
"</S:Envelope>";
}
}