Loading
HTTP Client

Java Quick Notes

Refresh Your Java - Before Java Interview

We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....


Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet

Go to SiteMap

Q)  How to use HTTP Client ?


Ans)

There are Several ways of doing this. Following most popular ways. We can also consider
this as REST Web services client.

i) By using HTTPConnectionURL :
public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL oracle = new URL("http://www.yourwebsite.com/");
        URLConnection yc = oracle.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader( yc.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine);
        in.close();
    }
}


ii) By using Apache HttpClient :
          HttpClient client = new DefaultHttpClient();
                HttpPost post = new HttpPost("http://yourhost/QueueTester/messageSender.do");
                try {
                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
                        nameValuePairs.add(new BasicNameValuePair("jmsMessage","http post"));
                        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                        HttpResponse response = client.execute(post);
                        BufferedReader rd = new BufferedReader(
      new InputStreamReader(response.getEntity().getContent()));

                        String line = "";
                        while ((line = rd.readLine()) != null) {
                                System.out.println(line);
                   }
        } catch (IOException e) {
                        e.printStackTrace();
                }



Back to top

------------------------- We hope you got necessary Info On -----------------------------------------

HTTP Client


HTTP Client

-------------------------------------------------------------------------------------------------------



Face Book
Request for a Mock Interview/Training

Get a PDF

Face Book
Same look (Read) on any device, this is Ads free