Java 异常…http异常403
有人能帮我吗?我正在使用下面的代码访问一个网页,我已经通过浏览器通过一些用户密码认证访问了该网页。 但当我尝试同样的方法时,我得到了403异常,我做错了吗Java 异常…http异常403,java,authentication,http-status-code-403,Java,Authentication,Http Status Code 403,有人能帮我吗?我正在使用下面的代码访问一个网页,我已经通过浏览器通过一些用户密码认证访问了该网页。 但当我尝试同样的方法时,我得到了403异常,我做错了吗 import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.net.PasswordAuthentication; import java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.PasswordAuthentication;
import java.net.Authenticator;
import java.net.InetAddress;
import java.lang.reflect.Proxy;
import java.lang.System;
import sun.misc.*;
import java.net.HttpURLConnection;
public class summa {
//static final String kuser = " "; // your account name
static final String kpass = " "; // your password for the account
//static class MyAuthenticator extends Authenticator {
//public PasswordAuthentication getPasswordAuthentication() {
// I haven't checked getRequestingScheme() here, since for NTLM
// and Negotiate, the usrname and password are all the same.
//System.err.println("Feeding username and password for " + //getRequestingScheme());
//return (new PasswordAuthentication(kuser, kpass.toCharArray()));
//}
//}
public static void main(String[] args) {
try {
URL google = new URL("http://www.google.com/");
HttpURLConnection yc =(HttpURLConnection)google.openConnection();
System.setProperty("http.proxyHost","171.160.82.70") ;
System.setProperty("http.proxyPort", "80") ;
String userPassword = " ";
String encoding = new sun.misc.BASE64Encoder().encode (userPassword.getBytes());
//Authenticator.setDefault(new MyAuthenticator());
yc.setRequestProperty ("Authorization", "Basic " + encoding);
yc.setRequestProperty(userPassword, encoding);
yc.addRequestProperty("User-Agent","ie-7 (compatible; MSIE 6.0; Windows NT 5.0)");
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine="s";
inputLine=in.readLine();
System.out.println(inputLine);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
参阅.< /P>请格式化代码,是否考虑使用一个已验证的HTTP客户端库?如果您试图用上面的代码访问谷歌,则需要附加一个注释:谷歌允许访问某些用户代理,java可能是其中之一。No.i为实例添加了谷歌。它真的随我们使用的url而变化吗?如果是的话,有没有办法检查它?如果你想确保你没有因此而被拒绝,请将你的用户代理更改为类似Firefox字符串的内容。是的,谷歌是一个很坏的例子,因为如果你不使用他们的API,他们对UA很挑剔。@jCoder:你不是说禁止吗?