一、基础进阶
- httpclient调用WebService
package com.demo;
import com.midea.gls.MobileCodeWS;
import com.midea.gls.MobileCodeWSSoap;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* \* Created with IntelliJ IDEA.
* \* Author: huyi
* \* Date: 2017/2/17 22:15
* \* Description:
* \
*/
public class WSHttpClient {
public static void main(String[] args) {
String mobileCode = "18680586568";
// get(mobileCode);
// post(mobileCode);
soap(mobileCode);
}
public static void get(String mobileCode){
HttpClient client = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://ws.webxml.com.cn//WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode="+mobileCode+"&userID=");
try {
HttpResponse response = client.execute(httpGet);
System.out.println("executing request " + httpGet.getURI());
// 获取响应实体
HttpEntity entity = response.getEntity();
System.out.println("--------------------------------------");
// 打印响应状态
System.out.println(response.getStatusLine());
if (entity != null) {
// 打印响应内容长度
System.out.println("Response content length: " + entity.getContentLength());
// 打印响应内容
System.out.println("Response content: " + EntityUtils.toString(entity));
}
System.out.println("------------------------------------");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void post(String mobileCodeVal){
HttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://ws.webxml.com.cn//WebServices/MobileCodeWS.asmx/getMobileCodeInfo");
try {
//设置参数
List list = new ArrayList();
list.add(new BasicNameValuePair("mobileCode",mobileCodeVal));
list.add(new BasicNameValuePair("userID",""));
UrlEncodedFormEntity requestEntiry = new UrlEncodedFormEntity(list);
httpPost.setEntity(requestEntiry);
HttpResponse response = client.execute(httpPost);
System.out.println("executing request " + httpPost.getURI());
// 获取响应实体
HttpEntity httpEntity = response.getEntity();
System.out.println("--------------------------------------");
// 打印响应状态
System.out.println(response.getStatusLine());
if (httpEntity != null) {
// 打印响应内容长度
System.out.println("Response content length: " + httpEntity.getContentLength());
// 打印响应内容
System.out.println("Response content: " + EntityUtils.toString(httpEntity));
}
System.out.println("------------------------------------");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void soap(String mobileVal){
MobileCodeWS mobileCodeWS = new MobileCodeWS();
MobileCodeWSSoap mobileCodeWSSoap = mobileCodeWS.getMobileCodeWSSoap();
String address = mobileCodeWSSoap.getMobileCodeInfo(mobileVal, null);
System.out.println(address);
}
}
- wsimport调用WSDL
wsimport -s . -p com.midea.gls http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL
找到WSDL中关于service的调用
public static void soap(String mobileVal){
MobileCodeWS mobileCodeWS = new MobileCodeWS();
MobileCodeWSSoap mobileCodeWSSoap = mobileCodeWS.getMobileCodeWSSoap();
String address = mobileCodeWSSoap.getMobileCodeInfo(mobileVal, null);
System.out.println(address);
}
二 、 发布自己的WSDL
发布自己的WSDL时候,需要注意,地址栏后面要自己加?WSDL
package com.demo;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
/**
* \* Created with IntelliJ IDEA.
* \* Author: huyi
* \* Date: 2017/2/20 13:32
* \* Description:
* \
*/
@WebService
public class MyWS {
public String sayHello(String name) {
String reVal = name + ", 你好!";
System.out.println(reVal);
return reVal;
}
public static void main(String[] args) {
String address = "http://10.74.96.29/ws";
Endpoint.publish(address , new MyWS());
System.out.println("发布的地址是:"+address);
}
}
访问http://10.74.96.29/ws?WSDL,可以见到
This XML file does not appear to have any style information associated with it. The document tree is shown below.
三 、客户端访问
- 首先在CMD中调用wsimport工具生成本地WebService代码
wsimport -s . -p com.ws http://10.74.96.29/ws?WSDL
- 导入代码到本地项目后,直接调用即可
public static void soap2(String mobileVal){
MyWS mobileCodeWS = new MyWS();
String name = "huyi";
String reVal = mobileCodeWS.sayHello(name);
System.out.println(reVal);
}
注意:一个端口可以发布多个Webservice服务
四 、ajax访问WebService
ajax不能直接访问WebService,因为存在跨域的问题,需要变通一下,通过ajax调用工程下的客户端访问WebService,客户端可以是一个servlet,或者spring下的controller
ws.jsp
<%--
Created by IntelliJ IDEA.
User: huyi
Date: 2017/2/20
Time: 15:05
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
$Title$
servlet
package com.gls.ws;
import com.demo.*;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* \* Created with IntelliJ IDEA.
* \* Author: huyi
* \* Date: 2017/2/20 15:50
* \* Description:
* \
*/
public class MyWsServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
com.demo.MyWS mobileCodeWS = new com.demo.MyWS();
String name = req.getParameter("name");
String reVal = mobileCodeWS.sayHello(name);
System.out.println(reVal+"------------");
resp.getWriter().print(reVal);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
web.xml
myWs
com.gls.ws.MyWsServlet
myWs
/myWsServlet