Share Sina's short chain to generate Java code

Source: Internet
Author: User

For example, you can convert http://zuidaima.com/to Http://t.cn/zlsvWVq

The following figure:

Original is not easy, reproduced please indicate the source share Sina short chain generated Java code

Package com.zuidaima.util;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStreamWriter;
Import java.net.HttpURLConnection;
Import Java.net.URL;
Import Java.net.URLEncoder;
Import Java.util.Map;
Import Java.util.LinkedHashMap;
Import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.regex.Matcher;

Import Java.util.regex.Pattern;

	public class Shortenurl {private static final Boolean DEBUG = true;
		public static void Main (string[] args) {String url = ' http://www.henshiyong.com/tools/sina-shorten-url.php ';
		map<string, string> params = new linkedhashmap<string, string> ();
		Params.put ("url", "http://www.google.com");

		Params.put ("Submit", "convert");

		String data = null;
			try {data = PostURL (URL, params);
			if (DEBUG) {System.out.println (data);
			} catch (IOException ex) {} if (data!= null) {String Shorturl = getshortenurl (data);
if (DEBUG) {				System.out.println (Shorturl);
		}} public static string Getshortenurl (string content) {string url = null;

		list<string> resultlist = getcontext (content); for (iterator<string> iterator = Resultlist.iterator (); iterator. Hasnext ();)
		{URL = iterator.next ();
	} return URL; /** * Extract "XXXX" from "<textarea>XXXX</textarea>" * * @param HTML * @return/Public stat
		IC list<string> getcontext (String html) {list<string> resultlist = new arraylist<string> ();
		Pattern p = pattern.compile ("<textarea> (. *) </textarea>");
		Matcher m = p.matcher (HTML);
		while (M.find ()) {Resultlist.add (M.group (1));
	return resultlist;
		public static class HttpException extends RuntimeException {private int errorcode;

		Private String ErrorData;
			Public HttpException (int errorcode, String errordata) {super ("HTTP Code" + errorcode + ":" + errordata);
			This.errorcode = ErrorCode;This.errordata = ErrorData;
		public int GetErrorCode () {return errorcode;
		Public String Geterrordata () {return errordata;  } public static string PostURL (string url, map<string, string> params) throws IOException {String data =
		""; For (String Key:params.keySet ()) {data + = "&" + Urlencoder.encode (Key, "UTF-8") + "=" + Urlencoder.encode p
		Arams.get (Key), "UTF-8");
		data = data.substring (1);
		SYSTEM.OUT.PRINTLN (data);
		URL Aurl = new Java.net.URL (URL);
		HttpURLConnection aconnection = (java.net.HttpURLConnection) aurl. OpenConnection ();
			try {aconnection.setdooutput (true);
			Aconnection.setdoinput (TRUE);
			Aconnection.setrequestmethod ("POST");
			Aconnection.setallowuserinteraction (FALSE); POST the data outputstreamwriter streamtoauthorize = new Java.io.OutputStreamWriter (Aconnection.getoutputstrea
			m ());
			Streamtoauthorize.write (data);
			Streamtoauthorize.flush (); Streamtoauthorize.close ();
			Check error int errorcode = Aconnection.getresponsecode ();
				if (ErrorCode >=) {InputStream errorstream = Aconnection.geterrorstream ();
					try {String errordata = streamtostring (Errorstream);
				throw new HttpException (ErrorCode, ErrorData);
				finally {errorstream.close ();
			}//Get the Response inputstream Resultstream = Aconnection.getinputstream ();
				try {String responsedata = streamtostring (Resultstream);
			return responsedata;
			finally {resultstream.close ();
		finally {aconnection.disconnect (); }} private static String streamtostring (InputStream resultstream) throws IOException {BufferedReader Areader = n
		EW Java.io.BufferedReader (New Java.io.InputStreamReader (Resultstream));
		StringBuffer aresponse = new StringBuffer ();
		String aLine = Areader.readline ();
			while (aLine!= null) {aresponse.append (aLine + "\ n");
		ALine = Areader.readline (); Return Aresponse.tostring ();

	    			 }
}
full Share code download address: http://www.zuidaima.com/share/1550463367924736.htm


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.