0

I am to ask your kind help for a problem I cannot solve... I have the following code:

      String https_url = "https://...";
      try {
        
        HttpsURLConnection connection = (HttpsURLConnection) new URL(https_url).openConnection();
        String query = "api_key=1234&portal_id=1234&username="+username;
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Length", String.valueOf(query.length())); 
        connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");      
        DataOutputStream output = new DataOutputStream(connection.getOutputStream());       
        output.writeBytes(query);       
        output.close();
        DataInputStream input = new DataInputStream( connection.getInputStream() ); 
        String ret = "";
        for( int c = input.read(); c != -1; c = input.read() )  {
            ret += (char)c ;
        }
        input.close(); 
        int code = connection .getResponseCode();
        String message = connection .getResponseMessage();
        System.out.println("Resp Code:"+code); 
        System.out.println("Resp Message:"+ message); 
        //...

This code has been running for a couple of years, and until some weeks ago, it has worked fine, then suddenly, it started to got a java.net.SocketException: Connection reset soon after the line output = new DataOutputStream(connection.getOutputStream()); This code runs into a Java web application, and it is called after the click on a button of a web page. I don't know if something has changed on server side, but the strange thing is that if I put this code into a main method of a stand alone class, export the class on an executable jar file, and launch the jar from the same environment where the web application is deployed, this code still works and I don't get any exception. Any suggestions? Thank you again Step

steprm
  • 11
  • 1
  • 5
  • While a connection reset is abnormal it's not what I would call rare. Network hiccups or server issues can cause it. If you've not had one for years you have been very lucky. – President James K. Polk Nov 15 '21 at 15:34
  • firs of all, thank you for the answer. I don't know if I have'not had any reset connection over the years, but for sure until some weeks ago it happened never or almost never. Now it happens always from the webapp... – steprm Nov 15 '21 at 16:13
  • Understood. If it were me I'd use network analysis tools like e.g. wireshark, running java with network debugging output enabled, etc. to see what's different between the two ways you've tried to use the code to connect. – President James K. Polk Nov 15 '21 at 17:47
  • For more on debugging java network issues see the [discussion of the javax.net.debug property](https://docs.oracle.com/en/java/javase/11/security/java-secure-socket-extension-jsse-reference-guide.html#GUID-31B7E142-B874-46E9-8DD0-4E18EC0EB2CF). – President James K. Polk Nov 15 '21 at 18:00

0 Answers0