背景:tomcat-5.5.12设置为远程服务器
服务端设置:
Step 1) 打开${tomcat_home}/conf/tomcat-users.xml
加入如下代码:
Step2)修改web.xml
Example Security Constraint Protected Area /test/* DELETE GET POST PUT test BASIC WebService Form-Based Authentication Area
对网站/test/下的任何请求采用基本安全认证
客户端编程:
客户端通过httpclient-2.0.2请求该url,方法如下:
HttpClient httpClient = new HttpClient(); Credentials defaultcreds = new UsernamePasswordCredentials("a", "b");httpClient.getState().setCredentials("WebService Form-Based Authentication Area","www.cat.cn",defaultcreds); String url = "http://www.cat.cn/test/index.do"; GetMethod method = new GetMethod(url); method.setDoAuthentication( true ); httpClient.executeMethod(method); String s = method.getResponseBodyAsString(); System.out.println(s);
原文地址: