我目前使用http://imdbapi.org的imdb api来获取有关影片的某些信息。当我使用API并尝试在java中打开这个url时,它给我一个403错误。该网址应该以JSON的形式返回数据。 这里是我到目前为止的代码(Java 7中):当我尝试打开一个URL时,为什么会出现403错误
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class Test {
public static void main(String[] args) {
URL url =null;
try {
url = new URL("http://imdbapi.org/?q=batman");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream is =null;
try {
is = url.openConnection().getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(is) );
String line = null;
try {
while((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(line);
}
}
这确实是奇怪的,因为这个网址为我工作。 – fge