简体   繁体   English

我尝试打开URL时为什么会收到403错误

[英]Why do I get a 403 error when I try open a URL

I am currently using the imdb api from http://imdbapi.org to get some information about a movie. 我目前正在使用http://imdbapi.org上的imdb api来获取有关电影的一些信息。 When I use the API and try to open this url in java it gives me a 403 error. 当我使用API​​并尝试在java中打开此URL时 ,它给出了403错误。 The url is supposed to return the data in JSON. url应该以JSON格式返回数据。 Here is my code so far(Java 7): 这是我到目前为止的代码(Java 7):

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);
    }
}

You should set User-Agent : 您应该设置User-Agent

System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.29 Safari/537.36"); 

or 要么

URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.29 Safari/537.36");
is = connection.getInputStream();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何摆脱这个错误? 当我尝试使用 SceneBuilder 打开时,它不起作用 - How do i get rid of this error? When i try to open with SceneBuilder it doesnt work 当我尝试在库存外部/没有物品的库存中单击时,为什么会出现错误? - Why do I get an error when I try to click outside the inventory / in the inventory where there's no item 为什么在尝试输出数组时会出现异常错误? - Why do I get an exception error when I try output an array? 当我尝试在基类方法中调用子类方法时,为什么会出现错误? - Why do I get an error when I try to call a subclass method inside a baseclass method? 当我尝试保存测试计划时,为什么会出现 NoClassDefFound 错误? - Why do I get a NoClassDefFound error when I try to save my test plan? 当我尝试使用具有相同名称和参数类型的两个方法时,为什么会出现编译错误? - Why do I get a compilation error when I try to have two methods with the same name and parameter type? 当我尝试使用我的jsf自定义标记时,为什么会出现错误“前缀[..]未定义”? - Why do I get error “prefix [..] is not defined” when I try to use my jsf custom tag? 尝试包含Urdu字符串时为什么会出现编译错误? - Why do I get a compilation error when I try to include an Urdu string? 尝试定义这三个构造函数时为什么会出错? 给出的例子 - Why do i get a error when i try to define these three constructors? Examples given 为什么会出现编译错误? 使用 try and catch - Why do I get compile error? using try and catch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM