Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass query parameters in java with Http client

Tags:

java

rest

http

get

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.

like image 540
Tapasweni Pathak Avatar asked Jul 02 '12 18:07

Tapasweni Pathak


1 Answers

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!");
              }
          }
   }
like image 182
manurajhada Avatar answered Oct 16 '22 16:10

manurajhada