'java URL working on browser but not in client program

Client JAVA Program part :

    try
                            {

                             String downloadUrl1="https://www......&downloaded=1";
                             url = new URL(downloadUrl1);
                             URLConnection myURLConnection = url.openConnection();
                             myURLConnection.connect();
                             updateMsg.setText("Success");
                             logger.debug(downloadUrl1);
                            }
                            catch (MalformedURLException e) {                              
                                // new URL() failed
                                logger.debug(e);
                            } 
                            catch (IOException e) {   
                                // openConnection() failed
                                logger.debug(e);
                            }

The Success Message is shown , the url is also printed in log file , no Exception is thrown .

Server side Program Part :

$downloaded_status_by_clnt = Sanitize::escape ( $_GET ['downloaded'] );

 if($downloaded_status_by_clnt=='1')
 {
     // Do some update                             
 }
 else
 {
    // Some file reading code
 }

 exit(0);

The update is working when I paste the URL in browser , but not working through program.

Can Anyone please suggest a solution ?



Solution 1:[1]

The Java code "connects" to the URL but does not attempt to read the content, or even check the status code.

If all you are trying to do is to connect, then the code works. If not ... will you need to add code to do the rest of ... whatever it is that you are trying to do.

There are numerous examples on the web of reading from a URL in Java. For example:

Solution 2:[2]

Check the HTTP proxy if you are running the code in your organization. Browser might already have the proxy.

You can use the System properties to provide proxy host and port. (There are other ways to do that as well)

-Dhttp.proxyHost=yourProxyHost -Dhttp.proxyPort=yourProxyPort -Dhttps.proxyHost=yourProxyHost -Dhttps.proxyPort=yourProxyPort

Solution 3:[3]

Spent several hours trying to solve the problem, and it turns out that I had to change the "User-Agent" header sent from Request. For instance,

HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("https://example.com"))
                .headers("User-Agent","Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36")      
                .POST(HttpRequest.BodyPublishers.noBody())
                .build();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 Optional
Solution 3 testing_22