Java Web site to the request sent POST

Author:Anonymous    Updated:2008-3-20 12:22:57
To a Web site can send POST requests need only simple steps:

First, and the URL of URLConnection dialogue. URLConnection can easily get from the URL. For example:

/ / Using java.net.URL and

/ / Java.net.URLConnection

URL url = new

URL ( "http://jobsearch.dice.com/jobsearch/jobsearch.cgi");

URLConnection connection = url.openConnection ();

Set then connect the output mode. URLConnection normally used as an input, such as downloading a Web page. URLConnection set by the output, you can put your data to a Web page transmission. Below is how to do:

...

Connection.setDoOutput (true);

Finally, in order to be OutputStream, and the sake of simplicity, and it locked in the Writer Add POST information, such as:

...

OutputStreamWriter out = newOutputStreamWriter (uc.getOutputStream (), "8859_1");

Out.write ( "username = bob & password =" + password +"");

/ / Remember to clean up

Out.flush ();

Out.close ();

This will send a POST looks like this:

POST / jobsearch / jobsearch.cgi HTTP 1.0

ACCEPT: text / plain

Content-type: application / x-www-form-urlencoded

Content-length: 99

Username = bob

Password = someword

Once sent successfully, the following method can be server response:

Connection.getInputStream ();

Some Web sites use the form rather than GET POST, it is because POST can carry more data, but no URL, which makes it look not so huge. Listed above generally use the code, Java code can be easily and the realization of these sites dialogue.



To a Web site can send POST requests need only simple steps:

First, and the URL of URLConnection dialogue. URLConnection can easily get from the URL. For example:

/ / Using java.net.URL and

/ / Java.net.URLConnection

URL url = new

URL ( "http://jobsearch.dice.com/jobsearch/jobsearch.cgi");

URLConnection connection = url.openConnection ();

Set then connect the output mode. URLConnection normally used as an input, such as downloading a Web page. URLConnection set by the output, you can put your data to a Web page transmission. Below is how to do:

...

Connection.setDoOutput (true);

Finally, in order to be OutputStream, and the sake of simplicity, and it locked in the Writer Add POST information, such as:

...

OutputStreamWriter out = newOutputStreamWriter (uc.getOutputStream (), "8859_1");

Out.write ( "username = bob & password =" + password +"");

/ / Remember to clean up

Out.flush ();

Out.close ();

This will send a POST looks like this:

POST / jobsearch / jobsearch.cgi HTTP 1.0

ACCEPT: text / plain

Content-type: application / x-www-form-urlencoded

Content-length: 99

Username = bob

Password = someword

Once sent successfully, the following method can be server response:

Connection.getInputStream ();

Some Web sites use the form rather than GET POST, it is because POST can carry more data, but no URL, which makes it look not so huge. Listed above generally use the code, Java code can be easily and the realization of these sites dialogue.
Previous:JPDA with easy debugging Java code
Next:Java application skills: explain the operation of the Cookie
User Reviews
Related Articles
Recommended article
AD