接口调用

一、http post方式调用:一般是调用别人接口,获取数据或者推送数据:

    方式一:使用 HttpClient 方式,发送json 或者  xml格式数据    包:(commons-httpclient-3.0.jar 

package com.cn.test;
import javax.xml.ws.http.HTTPException;
public class HttpUtils {
//传递json格式的数据
public static String post(String strData,String url) throws HttpException{
HttpClient httpClient = new HttpClient();
//设置连接超时
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(20000);
PostMethod method=new PostMethod(url);
method.setRequestHeader("Content-type","application/json; charset=utf-8");
RequestEntity enty=new StringRequestEntity(strData,"application/json","UTF-8");
method.setRequestEntity(enty);
httpClient.executeMethod(method);
return method.getResponseBodyAsString();
}

//传递xml格式的数据
public static String postByForm(String strData,String url) throws HttpException{
	HttpClient httpClient = new HttpClient();
	//设置连接超时
	httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(20000);
	PostMethod method=new PostMethod(url);
	method.setRequestHeader("Content-type","application/x-www-form-urlencoded; charset=utf-8");
	NameValuePair nvp=new NameValuePair("data",strData);
	NameValuePair[] nvps = { nvp };
	httpClient.executeMethod(method);
	return method.getResponseBodyAsString();
}

}

二、webservice方式:
首先,接口分为接口调用方、接口服务提供方:
接口调用方: 我调用别人的接口获取数据(推送数据),或着别人调用我的接口获取数据(推送数据给我)。
接口服务公共方:别人封装一个接口供我调用,或者别人封装一个接口供我调用。

    1、接口服务方:
        创建一个接口、实现类,然后将接口注入spring容器,最后将该接口发布服务。将接口地址、名称、参数告诉对方,对方就可
        以调用我的接口,推送给我数据,或者从我这获取数据。

                 1)、创建接口:
                           package com.cn.test;
                           import javax.jws.WebService;
                           @WebService
                           public interface IFininsclaService {
                            //接口方法
                            public String getInfo(String param);
                            }


                            
                  2)、创建接口实现类:

                            package com.cn.test;
                            import javax.jws.WebService;
                            @WebService(endpointInterface = "com.cn.test.IFininsclaService")
                            public class FinisclaServiceImpl  implements IFininsclaService{
                            public String getInfo(String param) {
	                        //业务逻辑
	                        return null;
                            }
                            }


                  3)、将接口注入spring容器:在bean_spring.xml文件中:

                            <bean id="IFininsclaService" class="com.cn.test.FinisclaServiceImpl"></bean>



                  4)、将该接口发布服务:在bean_webservice.xml文件中:

                            <jaxws:endpoint id="FinisclaServiceImpl" implementor="#IFininsclaService" address="/all/IFininsclaService"
                               publishedEndpointUrl="http://${WEBSERVICE_IP}/tkinfo/cc/all/IFininsclaService"></jaxws:endpoint>

                以上就是接口服务方封装接口的基本步骤。服务方不涉及客户端等等,只需创建接口封装逻辑就ok.





    2、webservice的接口调用方:
posted @ 2018-12-03 22:15  L_youlin  阅读(190)  评论(0)    收藏  举报