AnsweredAssumed Answered

error when executing a webscript from another application

Question asked by grosisimo on Dec 19, 2007
Latest reply on Dec 19, 2007 by grosisimo
Hi

I need to execute a webscript from Liferay (it isn't important the fact that the application is Liferay).

I was trying to do this making a request to the webscript url using this code

   private void sendRequest(String url)
   {
      try {
         URLConnection urlc = (new URL(url)).openConnection();
         urlc.connect(); //this line may be removed
         
         InputStream stream = urlc.getInputStream();
         int carac;
         
         while ((carac = stream.read()) != -1)
         {
            System.out.println(carac + " - ");
         }
         
         System.out.println("end");
         
         stream.close();
      } catch (MalformedURLException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   

The idea is just to make a call to the webscript url, so that it is executed.
(if someone knows a better way, please tell me).

Maybe it's not the best way, but it "is a valid solution" and it worked (it wasn't exactly this, but this function is simpler and has the same result).
The next day I tried it again, but in that moment I got an IOException "connection refused" when trying to get the inputstream. Since that moment I got that exception, and I don't know why.

After making a lot of tests, I saw that if I called openConnection() twice without closing the first connection, it worked!.
That was very strange. I thought it could be just a matter of "waiting some seconds before getting the InputStream", but I tried using a timer and It threw "connection refused too".

So, I decided not to worry and just open the connection twice.
After doing that, the webscript sometimes threw some authentication errors, but after repeating the same calls it worked.

   private void mandarPedido(String url)
   {
      try {
         byte [] data_bytes = {0}; //i need at least 1 byte
         URLConnection conec = (new URL(url)).openConnection();
         conec.connect();

         boolean threw_exception = false;
         InputStream stream = null;
         
         do
         {
            //repeat until it works!!

            try {
               threw_exception = false;
               stream = conec.getInputStream();
            } catch (IOException e) {
               conec = (new URL(url)).openConnection();
               conec.connect();
               
               if (e.getMessage().matches("^Server returned HTTP response code: [0-9]+ for URL:.*$"))
               {
                  //I read the webscript error code
                  int errcode = new Integer(e.getMessage().replaceFirst("^Server returned HTTP response code: ([0-9]+) for URL:.*$", "$1")).intValue();
                  threw_exception = (errcode == 500);
                  
                  if (errcode != 500)
                  {
                     //a real exception
                     throw e;
                  }
               }
               else if (stream == null)
               {
                  threw_exception = true;
               }
               else
               {
                  threw_exception = false;
                  throw e;
               }
            }
         } while (threw_exception);
            
         try
         {
            stream.read(data_bytes);
         }
         catch (NullPointerException npe)
         {
            npe.printStackTrace();
         }

         stream.close();
      } catch (MalformedURLException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

After writing all this code, I tried it and worked.

The next day I tried it again, ant this time the webscript threw "error 505", that means "the server doesn't accept the HTTP version" (????).

Now I'm absolutely lost.

Outcomes