2024年1月31日发(作者:)

Web service提供数据供别人使用,如果因为数据的保密性需添加认证,可以使用axis2提供的basic验证。

具体做法如下。

如果项目是基于axis2开发的,需修改文件

第一步:

Apache-Axis2

AxisServlet

Apache-Axis Servlet

rvlet

1

……….

……….

AxisServlet

/services/*

bank_member

BASIC

Axis Basic Authentication Area

bank_member

AxisServlet 是你的名,就是需要给那个servlet添加验证,这块的名字就要和那个名字对应

/services/* 是那些请求地址需要验证,例子中是services下的所有请求都需要验证

Bank_member表示角色名

只有在服务端属于bank_member角色(组)中的人员才被允许访问该web service且,该该访问采用“BASIC”模式,即需要用户名和密码来进行访问。

然后修改tomact 配置文件 cof文件夹下的文件

添加用户名密码:

好了,这样就可以了,修改完后重启tomcat。

输入web service地址,上面是对/service/* 下请求拦截,此时就会发现

输入正确的用户名和密码就会进入web service的wsdl描述页面,错误的话就会就如一个401错误页面。OK,这样就可以了,下来就是如何通过验证访问web service

电脑客户端访问代码如下:

public class AuthTest {

public static void main(String[] args) {

try {

ServiceClient sc = new ServiceClient();

Options opts = ions();

(new

EndpointReference("localhost:8080/axis2/services/PrizeInfoService/getAllCommentByPage"));

ion("urn:getAllCommentByPage");

ticator basicAuth = new

ticator();

rname("用户名");

sword("密码");

// t("");

// ain("");

perty(TICATE, basicAuth);

n(TICATE);

OMElement res = ceive(createPayLoad());

n(res);

} catch (AxisFault e) {

tackTrace();

}

}

public static OMElement createPayLoad() {

OMFactory fac = actory();

OMNamespace omNs =

OMNamespace("","m");

OMElement method = OMElement("getAllCommentByPage",

omNs);

//如果请求的方法需要参数,如下添加

OMElement value = OMElement("programNumber", null);

t("1");

ld(value);

n(method);

return method;

}

}

Android端是通过Ksoap访问web service的

代码如下:

首先

自定义一个类继承HttpTransportSE,我这块定义类名为MyHttpTransportSE。

在自定义类中复写getServiceConnection( )方法,代码如下

public class MyHttpTransportSE extends HttpTransportSE {

protected ServiceConnection getServiceConnection() throws

IOException {

//设置验证用户名和 密码

ServiceConnection connection = viceConnection();

}

源码中直接返回一个new ServiceConnection(),我们现在要访问有验证的web service,需要在创建连接时设置请求信息,代码就是上面的啦。

然后就是ksoap访问web service步骤啦,代码如下:

}

String Login = ("账号:密码".getBytes());

uestProperty("Authorization", "Basic " +

return connection;

public MyHttpTransportSE(String url) {

}

super(url);

Login);

public class Util {

//命名空间

final static String serviceNameSpace = "";

public final static String ERROR = "netError";//定义一个常量,表示异常返回信息

{

//如果调用的方法有参数,则给request中添加参数名,和参数,有多少加多少

perty(“参数名”,参数值);

// 获得序列化的Envelope

SoapSerializationEnvelope envelope = new

11);

String resultString = "";// 该变量用于保存接收到的字符串

// 实例化SoapObject对象

SoapObject request = new SoapObject(serviceNameSpace, method);

public static String getInfoFromWeb(String method, String address)

SoapSerializationEnvelope(

//这块的11要看你web service那端的版本了

t = request;

= true;

putSoapObject(request);

// 构建传输对象,并指明WSDL文档URL

String serviceURL =

"http:192.168.80.21:8080/axis2/services/PrizeInfoService/"+method);

// Android传输对象

MyHttpTransportSE ht = new MyHttpTransportSE(serviceURL);

}

别忘了加

好了,android端也能通过验证获取数据啦。

}

= true;

StringBuffer SOAP_ACTION = new StringBuffer();

SOAP_(serviceNameSpace);

SOAP_("/");

SOAP_(method);

try {

}

return resultString;

(SOAP_ng(), envelope);

// 如果获得信息

if ( != null) {

}

resultString = ERROR;

tackTrace();

resultString = ERROR;

tackTrace();

// 获取接收的数据

// 将传输结果包装成字符串返回

Log.e("receive msg", ng());

SoapObject soapObject = (SoapObject) ;

for (int i = 0; i < pertyCount(); i++) {

}

Log.e("receive msg", resultString);

resultString = perty(i).toString();

} catch (IOException e) {

} catch (XmlPullParserException e) {