一般服务端采用Basic方式作验证时,就会用到这个。
public void testCj{
private String username="test";
private String password="test"
static class MyAuthenticator extends Authenticator {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return (new PasswordAuthentication(username, password.toCharArray()));
}
}
@Test
public void test(){
Authenticator.setDefault(new MyAuthenticator());
URL url = new URL("");
HttpURLConnection conn =(HttpURLConnection)url.opentConnection();
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),5*1024*1024);
String next = null;
while ((next = reader.readLine()) != null) {
sb.append(next);
}
if (StringUtils.isBlank(sb.toString())) {
return null;
}else{
return sb.toString();
}
}
}