Automatic submission of HTTP form data using Java

Source: Internet
Author: User

Unified URL Encoding

package demo0808.demo2;/** * because the POST request for HTTP on the network needs to be encoded * To adapt to different operating systems * encoded into uniform ASCII characters * This class URL-encodes the query statement * Key-value pair Intermediate = connection * Different key-value pairs with & connection */import Java.net.urlencoder;public class Qu erystring {private StringBuilder query=new StringBuilder ();p rivate String charset= "UTF-8";/** * No parameter default constructor */public QueryString () {}/** * Sync add key value pair * @param name Parameter name * @param value parameter value */public synchronized void Add (String name,string val UE) {query.append (' & '); encode (name,value);} /** * Synchronize URL encoding * @param name Parameter name * @param value parameter value */private synchronized void encode (String name,string value) {try {Q Uery.append (Urlencoder.encode (name, CharSet)); Query.append (' = '); Query.append (Urlencoder.encode (Value,charset));} catch (Exception ex) {System.err.println ("bad VM can ' t-support" +charset);}} /** * Synchronously gets the query string * @return string */public synchronized Strings Getquery () {return query.tostring ();} /** * Overwrite ToString () method */public String ToString () {return getquery ();}} 

HTTP-based POST request key value pairs fill in

Package demo0808.demo2;/** * POST request for HTTP protocol * Form Auto-fill * does not consider Authenticode Identification * Protocol */import java.io.inputstream;import Java regardless of https type. Io. Outputstream;import Java.io.outputstreamwriter;import Java.net.url;import Java.net.urlconnection;public class formposter {Private URL url;private QueryString querystring=new QueryString ();/** * constructor * @param URL Uniform Resource location symbol */public F Ormposter (url url) {if (!url.getprotocol (). toLowerCase (). StartsWith ("http")) {System.err.println ("Post only for HTTP protocol! "); return;} This.url=url;} /** * Add parameter-value pair * @param name Parameter name * @param value parameter value */public void Add (String name,string value) {querystring.add (name, VA Lue);} /** * Get URL * @return URL */public url GetURL () {return URL;} /** * Get the appropriate input stream for the site * @return input stream * @throws Exception cannot connect exception */public InputStream post () throws Exception {URLConnection ope Nconnection = Url.openconnection (); Openconnection.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 5.0; Windows NT; Digext); Openconnection.setdooutput (true); OutputStream outputstReam = Openconnection.getoutputstream (); OutputStreamWriter outputstreamwriter = new OutputStreamWriter (OutputStream, "UTF-8"); Outputstreamwriter.write (querystring.tostring ()); Outputstreamwriter.write ("\ r \ n"); o Utputstreamwriter.flush (); Outputstreamwriter.close (); Refreshing the close stream is important only if it is closed to get the input stream return Openconnection.getinputstream ();}}




Test Code

Package demo0808.demo2;/** * Test code */import java.io.file;import Java.io.fileoutputstream;import java.io.InputStream; Import Java.io.inputstreamreader;import Java.io.outputstreamwriter;import Java.net.url;public class Test {public static void Main (string[] args) throws Exception {URL url = new URL ("Https://uniportal.huawei.com/uniportal/login.do"); Formposter formposter = new Formposter (URL);/** * Following is the test of Huawei Recruitment website Landing Interface */formposter.add ("Actionflag", "loginauthenticate"); Formposter.add ("Lang", "zh"); Formposter.add ("redirect", "Http://career.huawei.com/recruitment/login_index.html?") redirect=http://career.huawei.com/recruitment/"), Formposter.add (" UID "," User name "), Formposter.add (" Password "," password ") ; InputStream InputStream = Formposter.post (); InputStreamReader inputstreamreader = new InputStreamReader (InputStream) ; File File=new file ("outfile"); FileOutputStream FileOutputStream = new FileOutputStream (file); OutputStreamWriter OutputStreamWriter = new OutputStreamWriter (FileOutputStream, "UTF-8"); int C;while (C=inputstreamreader.read ())!=-1) {System.out.print ((char) c); Outputstreamwriter.write ((char) c);} Inputstream.close (); Outputstreamwriter.close (); System.out.println ();}}



Automatic submission of HTTP form data using Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.