Jun 18, 2011

Using Java to send HTTP Request with POST method


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

public static void sentHttpPostRequest(String url, String args) throws IOException {
    URLConnection connection = new URL(url).openConnection();
    // by default, connection with enable input, but won't enable output
    connection.setDoOutput(true);
    //connection.setDoInput(true);
    OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
    out.write(args);
    out.flush();
    out.close();
    /*
     * If the page has respond, uncomment these statements the retrieve the respond data
    InputStream is = connection.getInputStream();
    FileOutputStream fos = new FileOutputStream("respond.txt");
    byte[] buffer = new byte[1024];
    for (int length; (length = is.read(buffer)) > 0;) {
        fos.write(buffer, 0, length);
    }
    fos.close();
    is.close();
    */
}

sentHttpPostRequest("http://somewhere/somefile", "arg1=value1&arg2=value2")

This example shows the args parameter is a chain of connected arguments.
You can perform an automatic task of HTTP request to the target site rapidly.
However, "Don't Be Evil" when you are using this method


19 comments:

Srishthi B said...

Thank you. Found it interesting and useful. Java is a general purpose, high-level, class-based and object-oriented programming language. And we provide Java training Chennai at Fita.

geethu said...

To learn shorter programs when compared to Java then it is the right time to choose python training. There is increasing market demand for professionals who have completed python training.
python training in chennai | python training institutes in chennai

Andrew Son said...

Awesome post.You have clearly explained about java request process.It is really useful.I suggest my team members to visit this website.Keep ,posting like this.
Regards,
Best JAVA Training institute in Chennai |Java Courses in Chennai | J2EE Training in Chennai

Paul Miller said...

Excellent post!!Java is a platform independent general purpose computer programming language. This java can run on any virtual machine. James gosling is a developer of java at sun microsystem laboratory. It was origin by two programming language namely c and c++(c with classes). Thank you..!!
Regards,
JAVA Training in Chennai | JAVA Training

for IT the said...

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

Unknown said...

I have read your blog its very attractive and impressive. I like it your blog.
Java Training institute in velachery

Paul Miller said...

I agree with your thoughts!!! As the demand of java programming application keeps on increasing, there is massive demand for java professionals in software development industries. Thus, taking training will assist students to be skilled java developers in leading MNCs.Best Java Training in Chennai | JAVATraining Institutes in Chennai

Paul Miller said...

I feel satisfied with your blog, you have been delivering useful & unique information to our vision even you have explained the concept as deep clean without having any uncertainty, keep blogging.
web designing course in chennai|web designing training|web designing training in chennai

revathi said...

I have read your post. It is really helpful. Android Training Institute in Chennai | Android Training Institute in Velachery.

sivanesan said...

Nice post. Very interesting to read. Thank you for Sharing.
erp in chennai

Mirnalini Sathya said...


Thank you sharing the excellent post about JAVA programming. you helped me to gain more information on the JAVA language.
Best JAVA Training in Chennai | JAVA Course in Chennai

devo said...

Amazing post. Thank you for sharing such a wonderful information. Linux Training Institute in Chennai | Linux Training Institute in Velachery.

sunitha vishnu said...


This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
Android Training in Chennai
Ios Training in Chennai

Ajay Raj said...

Nice Posting...
RHCE Training in Chennai

Mohana M said...

Your information about Java is useful for me to know more technical information. Really very informative post you shared here. Keep sharing this type of informative blog. If anyone wants to become a Java professional learn Java Training in Bangalore. Nowadays Java has tons of job opportunities for all professionals.

Krishna Veni said...

Good one, Thanks for sharing your information..
Java Training institute in chennai | Best Java Training in velachery

preethi s said...


Thanks a lot! You made a new blog entry to answer my question; I really appreciate your time and effort.
java training in chennai |
java training institutes in chennai

Sanjana E said...

Your information about the HTTP request with the post method using java language to the targeted site was informative.
Java Training in Chennai

ijazz jazz said...

Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.

Java Training in Bangalore

Post a Comment

Twitter Delicious Facebook Digg Google Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Best Hostgator Coupon Code