/** * Sends an HTTP GET request to a url * * @param endpoint - The URL of the server. (Example: " http://www.yahoo.com/search") * @param requestParameters - all the request parameters (Example: "param1=val1¶m2=val2"). Note: This method will add the question mark (?) to the request - DO NOT add it yourself * @return - The response from the end point */ public static String sendGetRequest(String endpoint, String requestParameters) { String result = null; if (endpoint.startsWith("http://")) { // Send a GET request to the servlet try { // Send data String urlStr = endpoint; if (requestParameters != null && requestParameters.length () > 0) { urlStr += "?" + requestParameters; } URL url = new URL(urlStr); URLConnection conn = url.openConnection (); // Get the response BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuffer sb = new StringBuffer(); String line; while ((line = rd.readLine()) != null) { sb.append(line); } rd.close(); result = sb.toString(); } catch (Exception e) { e.printStackTrace(); } } return result; }
I have created this blog to help programmers who have just started their career thats why I have written all the tutorials from practical approach rather then theoretical. If anyone has any query related to programming don't hesitate to ask. My contact information is in my profile. Also I would like to thank all the websites/Individual(s) from whom I gathered the material.
Monday, September 26, 2011
Sends an HTTP GET request to a url
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment