java basic验证_java basic认证

博客展示了一段Java代码,用于进行Basic验证,通过设置用户名和密码,尝试访问指定URL获取输入流并读取内容。但运行时出现错误,返回HTTP响应码401,提示服务器拒绝访问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

packagecom.http;importjava.io.BufferedReader;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.net.Authenticator;importjava.net.PasswordAuthentication;...

package com.http;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.Authenticator;import java.net.PasswordAuthentication;import java.net.URL;public class Test{ static final String kuser = "";//用户名,测试已填写 static final String kpass = "";//密码,测试已填写 static class MyAuthenticator extends Authenticator { protected PasswordAuthentication getPasswordAuthentication() { return (new PasswordAuthentication(kuser, kpass.toCharArray())); } } public static void main(String[] args) throws Exception { Authenticator.setDefault(new MyAuthenticator()); URL url = new URL(" http://api.t.sohu.com/users/show.xml"); InputStream ins = url.openConnection().getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(ins)); String str; while((str=reader.readLine())!=null) { System.out.println(str); } }}出现的错误:Exception in thread "main" java.io.IOException: Server returned HTTP response code: 401 for URL: http://api.t.sohu.com/users/show.xml at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at com.http.Test.main(Test.java:27)

展开

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值