January 8, 2015 asvignesh 0Comment

HTTP GET

The HTTP GET method is used to retrieve (or read) a representation of a web resource. In the non-error ( aka Happy path ) path, GET returns a representation in form of XML or JSON and the HTTP Status code will be 200 (OK). In case of any error in the path, it most often returns a 404 (NOT FOUND) or 400 (BAD REQUEST).

According to the design of the HTTP specification, GET requests are used only to read data and not change it. Therefore, when used this way, they are considered safe. That is, they can be called without risk of data modification or corruption.

NOTE: DO NOT EXPOSE UNSAFE OPERATIONS VIA GET—IT SHOULD NEVER MODIFY ANY RESOURCES ON THE SERVER.

Read RESTFul web service 

public Object httpGet(String requestUrl) throws Exception {
HttpURLConnection conn = (HttpURLConnection) new URL(requestUrl).openConnection();
conn.setRequestMethod("GET");
BufferedReader rd = null;
try {
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
return sb.toString();
} catch (Exception e) {
//Handle Exception
} finally {
if (rd != null)
rd.close();
}
}

 

public class VigneshHttpGetter {

// Constructor 
// All member variables

    static {
        try {
            TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                @SuppressWarnings("unused")
                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }

                @SuppressWarnings("unused")
                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }

                @Override
                public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
                    throws CertificateException {
                }

                @Override
                public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
                    throws CertificateException {
                }
            } };

            // Install the all-trusting trust manager
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

            // Create all-trusting host name verifier
            HostnameVerifier allHostsValid = new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            };

            // Install the all-trusting host verifier
            HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
        } catch (Exception e) {
            e.getMessage();
        }
    }

 

Leave a Reply

Your email address will not be published. Required fields are marked *