java 实现WebService

时间:2023-03-10 00:12:16
java 实现WebService

1.xml
    2.    wsdl: webservice description language web服务描述语言
        通过xml格式说明调用的地址方法如何调用,可以看错webservice的说明书
    
    3.soap simple object access protoacl (简单对象访问协议)
        限定了xml的格式
        soap 在http(因为有请求体,所以必须是post请求)的基础上传输xml数据
            请求和响应的xml 的格式如:    <Envelop>
                                <body>
                                //....
                                </body>
                            </Envelop>
                operation name:服务提供的方法
                
        
    静态方法不能发布为外部服务

请求过程分析:
        1.使用get方式获取wsdl文件,称为握手
        2.使用post发出请求
        3.服务器响应成功过
java 实现WebService    

几种监听工具:
    http watch
    Web Service explorer
    eclipse 自带工具   TCP/IP Monitor

(来源:https://www.cnblogs.com/siqi/archive/2013/12/15/3475222.html)

1.

public JSONObject getDataFromMingTong(String PoorQualityTime,String cellName,String ECI) throws Exception{
final String smsUrl = "http://10.195.156.212/CMGZ/TopCell/getKpiOfPoorQualityCell.ashx" ;
try {
URL url = new URL(smsUrl+"?param={\"ECI\":"+ECI+",\"CellName\":\""+URLEncoder.encode(cellName, "UTF-8")+"\",\"PoorQualityTime\":\""+PoorQualityTime+"\"}") ;
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Charset", "UTF-8");
OutputStream os = conn.getOutputStream();
InputStream is = conn.getInputStream();
byte[] b = new byte[1024*1024];
int len = 0;
String rst = "";
while((len = is.read(b)) != -1){
String ss = new String(b,0,len,"UTF-8");
rst += ss;
}
System.out.println("-----------新方法--------------------");
System.out.println(rst);
is.close();
os.close();
conn.disconnect(); JSONObject obj=new JSONObject();
obj=(JSONObject) JavaJsonUtil.jsonToBean(rst);
System.out.println("json转bean结果:-------");
System.out.println(obj); return obj;
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
return null;
}

2.

package com.eastcom_sw.inas.common.service.auth;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map; @Component("smsAuthServiceGZ")
@Transactional
public class SmsAuthServiceGz extends BaseAuthService{
protected static final Log logger = LogFactory.getLog(SmsAuthServiceGz.class); @Autowired
private UserService userService;
@Autowired
private SmsAuthDao smsAuthDao;
public AuthResponse auth(IntegeratedAuthReq req){
AuthResponse resp = new AuthResponse();
try {
logger.info("req param:"+req.getParams());
Map<String, String> params = req.getParams();
String optReason = null;
String leaderName = null;
String menuName = null;
if(params!=null){
optReason = params.get("optReason");
leaderName = params.get("leaderName");
menuName = params.get("menuName");
System.out.println("==============menuName:"+menuName);
//leaderPsd = params.get("leaderPsd");
}
String mobileNo=req.getMobileNo();
String msg="";
boolean authFlag=false;
MAUTH_LIST mlist = new MAUTH_LIST();
MAUTH_LOGS mlogs = new MAUTH_LOGS();
if(StringUtils.isNotBlank(mobileNo)){
String randomNum = (int) (Math.random()*9000+1000)+"";
User u = userService.loadUserByUsername(leaderName);
String url = "http://10.195.165.27:8095/INASSMSService/services/MsgService?wsdl" ; Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(url) ;
call.setOperation("sendMsg");
call.setTimeout(320*1000); //超时时间
String sendMsg = "用户"+req.getUserName()+",申请查看"+req.getMobileNo()+"号码日志,申请理由"+optReason+",短信验证码"+randomNum+",同意请回复验证码(5分钟内有效)!" ; } } catch (Exception e) {
CommonRsUtil.extractException(e, resp);
}
return resp;
}
}

3.

package com.eastcom_sw.inas.workorder.rest.service;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.rmi.RemoteException;
import java.util.List; import javax.xml.rpc.ServiceException; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;import net.sf.json.JSONObject; @Component
@Transactional(readOnly = true)
public class WorkorderSendMsgService extends BaseService{
@Autowired
private WorkorderSendMsgDao workorderSendMsgDao; public void sendMsgOfOrderOverTime(String t_time) {
try {
int i=0;
//告警
String ctrTime="SYSDATE - T.ORDER_TIME >=5 AND SYSDATE - T.ORDER_TIME <=7";
List<JSONObject> list1 = workorderSendMsgDao.getOverTimeOrderData(ctrTime,t_time);
for (JSONObject jsonObject : list1) {
String total=jsonObject.getString("total");
String order_typedes=jsonObject.getString("order_typedes");
if(order_typedes.equals("无线网")){
String wuxian_phones=WorkorderSendMsgCommonUtils.getWuxianPhones(this); SendMsg(wuxian_phones,total,"即将超时");//发送的电话号码、工单条数
}
else if(order_typedes.equals("核心网")){
String hexin_phones=WorkorderSendMsgCommonUtils.getHexinPhones(this); SendMsg(hexin_phones,total,"即将超时");//发送的电话号码、工单条数
}
else if(order_typedes.equals("业务网")){
String yewu_phones=WorkorderSendMsgCommonUtils.getYewuPhones(this); SendMsg(yewu_phones,total,"即将超时");//发送的电话号码、工单条数
}
} }catch (Exception e) {
e.printStackTrace();
}
} public void SendMsg(String phones,String total,String msgt) throws ServiceException, RemoteException{
// //长短信 短信批量发送,多个号码已逗号分隔
final String smsUrl = "http://10.195.165.27:8095/INASSMSService/SmsServlet" ;
boolean success = true ;
try {
String sendMsg = "您好!您有"+total+"条集中性能工单"+msgt+",请及时处理!" ;
URL url = new URL(smsUrl+"?phones="+phones+"&msg="+URLEncoder.encode(sendMsg, "utf-8")+"&callback=jobTaskCallBack_"+System.currentTimeMillis()) ;
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0"); OutputStream os = conn.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.flush();
dos.close(); InputStream is = conn.getInputStream();
DataInputStream dis = new DataInputStream(is);
byte d[] = new byte[dis.available()];
dis.read(d);
String rst = new String(d,"utf-8");
System.out.println("rst======="+rst);
if (rst.contains("短信发送成功!")) {
success = true ;
System.out.println("长短信 : "+phones+"||"+sendMsg+" [发送成功!]") ;
}else {
success = false ;
System.out.println("长短信 : "+phones+"||"+sendMsg+" [发送失败:"+rst+"]!") ;
}
conn.disconnect();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}

(代码取自部分项目例子,代码只做参考)