client调用restful,服务端有密码,client怎么写
漆黑之勺 2015-09-02 01:16:57 现在想用client去调用restful,但是服务(jboss)设有密码,client端怎么写才可以通过验证。
现在调用如果没有密码的话是这么调用的
private static String serverUri = "https://localhost:8443/ssh2/rest/json/user";
public final static String METHOD_GET="GET";
public final static String METHOD_PUT="PUT";
public final static String METHOD_DELETE="DELETE";
public final static String METHOD_POST="POST";
public static void rest(String serviceUrl,String parameter,String restMethod){
try {
URL url= new URL(serviceUrl);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod(restMethod);
if(!METHOD_GET.equals(restMethod)){
con.setDoOutput(true);
if(!METHOD_DELETE.equals(restMethod)){ //请求方法为PUT或POST时执行
OutputStream os = con.getOutputStream();
os.write(parameter.getBytes("UTF-8"));
os.close();
}
}
else{
InputStream in= con.getInputStream();
byte[] b= new byte[1024];
int result= in.read(b);
while( result!=-1){
System.out.write(b,0,result);
result= in.read(b);
}
}
System.out.println(con.getResponseCode()+":"+con.getResponseMessage());
} catch ( Exception e ) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
rest(serverUri,null,METHOD_GET);
}
如果在此基础上加入用户名和密码验证才可以访问到,如何解决