Getting HTTP 503 error while accessing the url from my Java program

I am creating a Java application that will download an HTML page from a website and save the file on my local system. I can manually access the URL of a webpage through a browser. But when I try to access the same URL in my Java program, the server returns error 503. Here's the script:

sample URL = http://content.somesite.com/demo/somepage.asp

Ability to access the above URL through a browser. But the Java code below cannot load the page:

StringBuffer data = new StringBuffer();
BufferedReader br = null;
try {
    br = new BufferedReader(new InputStreamReader(sourceUrl.openStream()));
    String inputLine = "";
    while ((inputLine = br.readLine()) != null) {
        data.append(inputLine);
    }
} catch (Exception e) {
    e.printStackTrace();
} finally {
    br.close();
}

So my questions are:

  • Am I doing something wrong here?

  • Is there a way for the server to block requests from programs / bots and allow only requests coming from browsers?

+3
1

HTTP User-Agent Referer - , -.

User-Agent : Seehowitruns: .

, , cookie, .

+3

Source: https://habr.com/ru/post/1728421/


All Articles