Java中,使用HTTP Basic进行验证
16lz
2021-01-26
import java.net.Authenticator; import java.net.PasswordAuthentication; public class fanfouauth extends Authenticator { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("用户名","密码".toCharArray()); } }
使用时 以饭否的API为例,每一次在使用URL的时候,调用Authenticator.setDefault一下.就可以了~
Authenticator.setDefault(new fanfouauth()); URL url = new URL("http://api.fanfou.com/account/verify_credentials.json"); BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); StringBuffer sb = new StringBuffer(""); String str=""; while((str = reader.readLine())!= null) sb.append(str); return sb.toString();
更多相关文章
- android、webService 天气预报demo
- OKhttp上传头像,调用相机相册进行裁剪
- Android调用邮件客户端
- Android(安卓)打开资源raw文件
- 模拟按键操作的几种方式
- 解析#android.view.InflateException#
- android 登录界面 记住多组账号与密码
- 【备忘】Android模拟小球自由落体(SurfaceView)
- RxJava2源码分析