Java automatic login program segment

Source: Internet
Author: User
package auto;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.net.URL;import java.net.URLConnection;public class AutoPost {public static void autoPost(String surl) throws IOException {URL url = new URL(surl);URLConnection conn = url.openConnection();conn.setDoOutput(true);OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");out.write("username=oscar999&password=");out.flush();out.close();StringBuffer sTotalStrbuf = new StringBuffer();String sLine = "";InputStream instream = conn.getInputStream();BufferedReader reader = new BufferedReader(new InputStreamReader(instream));while ((sLine = reader.readLine()) != null) {sTotalStrbuf.append(sLine);}System.out.println(sTotalStrbuf.toString());}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubString surl = "http://www.open-open.com/doc/members/login";try {autoPost(surl);} catch (IOException e) {e.printStackTrace();}}}

Improved Version 1

package auto;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.net.URL;import java.net.URLConnection;import java.util.HashMap;import java.util.Iterator;import java.util.Map;public class AutoPost {public static void autoPost(String surl, HashMap paraMhm)throws IOException {URL url = new URL(surl);URLConnection conn = url.openConnection();// set User-Agent, avoid occur 403 errorconn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");conn.setDoOutput(true);// OutputStreamWriter out = new// OutputStreamWriter(conn.getOutputStream(),// "UTF-8");OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(),"GB2312");StringBuffer sParameterStr = new StringBuffer("?");if (paraMhm != null && paraMhm.size() > 0) {Iterator it = paraMhm.entrySet().iterator();while (it.hasNext()) {Map.Entry entry = (Map.Entry) it.next();String sKey = (String) entry.getKey();String sValue = (String) entry.getValue();sParameterStr.append(sKey);sParameterStr.append("=");sParameterStr.append(sValue);sParameterStr.append("&");}// delete the last &sParameterStr.deleteCharAt(sParameterStr.length() - 1);}out.write(sParameterStr.toString());out.flush();out.close();StringBuffer sTotalStrbuf = new StringBuffer();String sLine = "";InputStream instream = conn.getInputStream();BufferedReader reader = new BufferedReader(new InputStreamReader(instream));while ((sLine = reader.readLine()) != null) {sTotalStrbuf.append(sLine);}System.out.println(sTotalStrbuf.toString());}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub// String surl = "http://www.open-open.com/doc/members/login";HashMap<String, String> paraHashMap = new HashMap<String, String>();// String surl = "http://passport.csdn.net/account/login";// paraHashMap.put("u", "oscar999");// paraHashMap.put("p", "");// String surl = "http://www.open-open.com/home/index.php";// paraHashMap.put("username", "oscar999");// paraHashMap.put("password", "");String surl = "http://www.e-works.com.cn/comm/ewk_business/usercenter/userlogin_comm.aspx";paraHashMap.put("tboxUserName", "oscar999");paraHashMap.put("tboxPwd", "");try {autoPost(surl, paraHashMap);} catch (IOException e) {e.printStackTrace();}}}

Use awt to write an interface when you have time

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.