10

I am using GET REST call in java using the given code, but I am getting an error code : 404 i.e Not Found . But when I am using the same URL in the browser I am getting the output and it is working fine.I new to JAVA. May be I am passing the query parameters wrongly, but I am not getting it. I am working in NETBEANS 7.1.2. Please help.

    import java.io.IOException;
    import java.io.OutputStreamWriter; 
    import java.net.HttpURLConnection; 
    import java.net.URL; 
       public class Test {
          private static String ENDPOINT ="http://wisekar.iitd.ernet.in/active/api_resources.php/method/mynode?";
          public static void main(String[] args) throws IOException 
          { 
              URL url = new URL(ENDPOINT + "key=" + "mykey"  );
              HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); 
              httpCon.setDoOutput(true);
              httpCon.setRequestMethod("GET");
             OutputStreamWriter out = new OutputStreamWriter( httpCon.getOutputStream());
             System.out.println(httpCon.getResponseCode());
             System.out.println(httpCon.getResponseMessage());
             out.close(); 
          }
   }

here mykey is the key given to me by the website.

I also want to print the response message on the output window or console. As I want to store it in the future for some extraction. Please Help.

0

1 Answer 1

12

Here is your code.. Use it. It is giving response as 401-Unauthorised to me and the same response at browser URL, may this cause of VPN some some other issue. IF you use

private static String ENDPOINT ="http://google.com"; 

It will give you 200-OK.

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

       public class Test {
          private static String ENDPOINT ="http://wisekar.iitd.ernet.in/active/api_resources.php/method/mynode";
          public static void main(String[] args) throws IOException 
          { 
              String url = ENDPOINT;
              String charset = "UTF-8";
              String param1 = "mykey";

              String query = String.format("key=%s", 
                   URLEncoder.encode(param1, charset));
              java.net.URLConnection connection = new URL(url + "?" + query).openConnection();
              connection.setRequestProperty("Accept-Charset", charset);
              if ( connection instanceof HttpURLConnection)
              {
                 HttpURLConnection httpConnection = (HttpURLConnection) connection;
                 System.out.println(httpConnection.getResponseCode());
                 System.out.println(httpConnection.getResponseMessage());
              }
              else
              {
                 System.err.println ("error!");
              }
          }
   }
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, I read about webservices (post and get) and got some good knowledge on it. And I have used soapUI tool to do GET service call , which is pretty easy job. SInce it is a tool it works fine without too much code writing. looking above code I think I can use HTTPClient and HTTPUrlconnection CLASSES and write java code to replace inputs when necessary and make some GET and POST calls. Please let me know whether it is good thing to do if i do not want to use SOAPUI tool.
yes you can use it.. Its all about the call frequency and data load.
Thanks for fast reply; could you please elaborate about call frequency ?
The repeatability of call to the destination URL by your code/program or the frequency of request to the server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.