Server returning 403 for url openStream()

前端 未结 3 1685
故里飘歌
故里飘歌 2021-01-04 22:50
import java.net.URL;
import java.io.*;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Test {
           


        
相关标签:
3条回答
  • 2021-01-04 23:25

    This is an old post but if people wanted to know how this works.

    a 403 means acces-denied. There is a work around for this. If you want to able to do this you have to set a user agant parameter to 'fool' the website

    This is how my old method looked like:

    private InputStream read() {
    try {
        return url.openStream();
     } 
    catch (IOException e) {
      String error = e.toString();
      throw new RuntimeException(e);
     }
    }
    

    Changed it to: (And it works for me!)

    private InputStream read() {
    try {
        HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
        httpcon.addRequestProperty("User-Agent", "Mozilla/4.0");
    
      return httpcon.getInputStream();
     } catch (IOException e) {
        String error = e.toString();
      throw new RuntimeException(e);
     }
    }
    
    0 讨论(0)
  • 2021-01-04 23:37

    Your mistake is swallowing the exception.

    When I run my code, I get an HTTP 403 - "forbidden". The web server won't allow you to do this.

    My code works perfectly for http://www.yahoo.com.

    Here's how I do it:

    package url;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.Reader;
    import java.net.URL;
    
    /**
     * UrlReader
     * @author Michael
     * @since 3/20/11
     */
    public class UrlReader {
    
        public static void main(String[] args) {
            UrlReader urlReader = new UrlReader();
    
            for (String url : args) {
                try {
                    String contents = urlReader.readContents(url);
                    System.out.printf("url: %s contents: %s\n", url, contents);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    
    
        public String readContents(String address) throws IOException {
            StringBuilder contents = new StringBuilder(2048);
            BufferedReader br = null;
    
            try {
                URL url = new URL(address);
                br = new BufferedReader(new InputStreamReader(url.openStream()));
                String line = "";
                while (line != null) {
                    line = br.readLine();
                    contents.append(line);
                }
            } finally {
                close(br);
            }
    
            return contents.toString();
        }
    
        private static void close(Reader br) {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-04 23:39

    This is now a completely different question so I have edited your title.

    According to your edit, you aren't getting null pointer exceptions, you are getting HTTP 403 status, which means 'Forbidden', which means you can't access that resource.

    0 讨论(0)
提交回复
热议问题